程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Imageai continued deepstack (II) fast and simple object detection using Python

編輯:Python

One . brief introduction

DeepStack Introduction and preparation for installation and operation
See the first article :
ImageAI To continue -DeepStack( One ) Use Python Fast and simple face detection 、 Face matching 、 Face comparison

Two . Object detection

1. start-up

Face recognition Used VISION-FACE function This time, object detection needs 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. function

2.1 adopt url call

import requests
from PIL import Image
import matplotlib.pyplot as plt
import cv2
def useUrl():
host = "http://192.168.0.101:8080"
# Load the picture to be tested 
image_data = open("1.jpg","rb").read()
img = cv2.imread("1.jpg")
# call http://192.168.0.101:8080/v1/vision/detection Detect the objects in the picture 
response = requests.post(host+"/v1/vision/detection",files={
"image":image_data}).json()
# Mark the test results in the picture 
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 adopt python sdk call

from deepstack_sdk import ServerConfig, Detection
def pythonsdk():
config = ServerConfig("http://192.168.0.101:8080")
detection = Detection(config)
## Detect objects in the picture 
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


Currently supported object types

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.

Complete code

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)
## Detect objects in the picture 
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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved