ImageAI续-DeepStack(二) 使用Python快速简单实现物体检测

一. 简介

DeepStack的简介及安装运行准备等
参见第一篇文章:
ImageAI续-DeepStack(一) 使用Python快速简单实现人脸检测、人脸匹配、人脸比较

二. 物体检测

1. 启动

上一篇人脸识别 使用了VISION-FACE功能 这一次物体检测需要VISION-DETECTION

#linux
docker run -e VISION-DETECTION=True -e MODE=High -v localstorage:/datastore -p 8080:5000 deepquestai/deepstack

#windows
deepstack --VISION-DETECTION True --PORT 8080

2. 运行

2.1 通过url调用

import requests
from PIL import Image
import matplotlib.pyplot as plt
import cv2

def useUrl():
    host = "http://192.168.0.101:8080"
    # 加载待测图片
    image_data = open("1.jpg","rb").read()
    img = cv2.imread("1.jpg")
    
    # 调用 http://192.168.0.101:8080/v1/vision/detection 检测图片内物体
    response = requests.post(host+"/v1/vision/detection",files={"image":image_data}).json()
    # 将检测结果在图片内框出标注
    font = cv2.FONT_HERSHEY_SIMPLEX
    for obj in response["predictions"]:
        print(obj)
        conf = obj["confidence"]*100
        label = obj["label"]
        y_max = int(obj["y_max"])
        y_min = int(obj["y_min"])
        x_max = int(obj["x_max"])
        x_min = int(obj["x_min"])
        pt1 = (x_min,y_min)
        pt2 = (x_max,y_max)
        cv2.rectangle(img,pt1,pt2,(255,0,0),2)
        cv2.putText(img,'{} {:.2f}'.format(label,conf),(x_min,y_min-15),font,1,(255,0,0),4)
    plt.imshow(img)
    plt.axis('off')
    plt.show()

请添加图片描述
请添加图片描述

2.1 通过python sdk调用

from deepstack_sdk import ServerConfig, Detection
def pythonsdk():
    config = ServerConfig("http://192.168.0.101:8080")
    detection = Detection(config)
    
    ##检测图片中物体
    response=detection.detectObject("2.jpg",output="2_output.jpg")
    for obj in response:
        print("Name: {}, Confidence: {}, x_min: {}, y_min: {}, x_max: {}, y_max: {}".format(obj.label, obj.confidence, obj.x_min, obj.y_min, obj.x_max, obj.y_max))

请添加图片描述

Name: person, Confidence: 0.5288314, x_min: 195, y_min: 220, x_max: 464, y_max: 816
Name: horse, Confidence: 0.5871692, x_min: 135, y_min: 220, x_max: 475, y_max: 821
Name: dog, Confidence: 0.9199933, x_min: 105, y_min: 496, x_max: 350, y_max: 819

请添加图片描述
当前支持检测的物体类型

person, bicycle, car, motorcycle, airplane,
bus, train, truck, boat, traffic light, fire hydrant, stop_sign,
parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant,
bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase,
frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove,
skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork,
knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot,
hot dog, pizza, donot, cake, chair, couch, potted plant, bed, dining table,
toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave,
oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear,
hair dryer, toothbrush.

完整代码

import requests
from PIL import Image
import matplotlib.pyplot as plt
import cv2

def useUrl():
    host = "http://192.168.0.101:8080"
    image_data = open("1.jpg","rb").read()
    img = cv2.imread("1.jpg")
    
    response = requests.post(host+"/v1/vision/detection",files={"image":image_data}).json()
    font = cv2.FONT_HERSHEY_SIMPLEX
    for obj in response["predictions"]:
        print(obj)
        conf = obj["confidence"]*100
        label = obj["label"]
        y_max = int(obj["y_max"])
        y_min = int(obj["y_min"])
        x_max = int(obj["x_max"])
        x_min = int(obj["x_min"])
        pt1 = (x_min,y_min)
        pt2 = (x_max,y_max)
        cv2.rectangle(img,pt1,pt2,(255,0,0),2)
        cv2.putText(img,'{} {:.2f}'.format(label,conf),(x_min,y_min-15),font,1,(255,0,0),4)
    plt.imshow(img)
    plt.axis('off')
    plt.show()
        
    
    
from deepstack_sdk import ServerConfig, Detection
def pythonsdk():
    config = ServerConfig("http://192.168.0.101:8080")
    detection = Detection(config)
    
    ##检测图片中物体
    response=detection.detectObject("2.jpg",output="2_output.jpg")
    for obj in response:
        print("Name: {}, Confidence: {}, x_min: {}, y_min: {}, x_max: {}, y_max: {}".format(obj.label, obj.confidence, obj.x_min, obj.y_min, obj.x_max, obj.y_max))

    

    
useUrl()
pythonsdk()

版权声明:本文为CSDN博主「alphaTao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zkt286468541/article/details/120825924

alphaTao

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

针对小目标的目标增强算法

针对小目标的目标增强算法 自己写的数据增强算法,请随意取用,包括椭圆旋转,随机复制。 找了好多并没有找到椭圆旋转和随机复制的代码,于是就自己写了一个,程序中自动生成anno

GiraffeDet:Heavy Neck的目标检测框架

关注并星标 从此不迷路 计算机视觉研究院 公众号ID|ComputerVisionGzq 学习群|扫码在主页获取加入方式 获取论文:关注并回复“GD” 计算机视觉研究院专栏 作者:Edison_G 在传统的目标检测框架中,从图像识别模型继承的主