Skip to content

Commit 5243c53

Browse files
committed
added object detection async sample app
1 parent f04e395 commit 5243c53

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

iewrap.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ def createInputBlobDict(self, inputData):
7575

7676
def asyncInfer(self, img):
7777
status = None
78-
while status!=0 and status!=-11:
78+
while status!=0 and status!=-11: # Find an idle infer_request
7979
req = self.execNet.requests[self.inferSlot]
8080
self.inferSlot = (self.inferSlot+1) % self.numRequests
8181
status = req.wait(-1)
8282
infID = self.inferenceID
83-
83+
8484
inBlobDict = self.createInputBlobDict(img)
8585
req.set_completion_callback(self.callback, (infID, req))
8686
req.async_infer(inputs=inBlobDict)
@@ -109,3 +109,7 @@ def blockInfer(self, img):
109109
else:
110110
return res # if the model has multiple outputs, return the result in dictionary
111111
# e.g. ( {'prob': array[], 'data': array[]})
112+
113+
def dummyInfer(self):
114+
for req in self.execNet.requests:
115+
req.infer()

iewrap_object_detection_async.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import iewrap
2+
3+
import time
4+
5+
import cv2
6+
import numpy as np
7+
8+
imgBuf = {}
9+
label = []
10+
11+
def callback(infId, output):
12+
global imgBuf, label
13+
14+
# Draw bounding boxes and labels onto the image
15+
output = output.reshape((100,7))
16+
img = imgBuf.pop(infId)
17+
img_h, img_w, _ = img.shape
18+
for obj in output:
19+
imgid, clsid, confidence, x1, y1, x2, y2 = obj
20+
if confidence>0.6: # Draw a bounding box and label when confidence>0.8
21+
x1 = int(x1 * img_w)
22+
y1 = int(y1 * img_h)
23+
x2 = int(x2 * img_w)
24+
y2 = int(y2 * img_h)
25+
cv2.rectangle(img, (x1, y1), (x2, y2), (0,255,255), thickness=4 )
26+
cv2.putText(img, label[int(clsid)][:-1], (x1, y1), cv2.FONT_HERSHEY_PLAIN, fontScale=4, color=(0,255,255), thickness=4)
27+
cv2.imshow('result', img)
28+
cv2.waitKey(1)
29+
30+
def main():
31+
global imgBuf, label
32+
label = open('voc_labels.txt').readlines()
33+
34+
cap = cv2.VideoCapture(0)
35+
cap.set(cv2.CAP_PROP_FRAME_WIDTH , 640)
36+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
37+
38+
#cap = cv2.VideoCapture('../sample-videos/people-detection.mp4')
39+
40+
ie = iewrap.ieWrapper('public/mobilenet-ssd/FP16/mobilenet-ssd.xml', 'CPU', 10)
41+
ie.setCallback(callback)
42+
43+
while True:
44+
ret, img = cap.read()
45+
if ret==False:
46+
break
47+
refId = ie.asyncInfer(img) # Inference
48+
imgBuf[refId]=img
49+
#time.sleep(1/30)
50+
51+
if __name__ == '__main__':
52+
main()

0 commit comments

Comments
 (0)