JeVoisBase  1.16
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
test-model.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 
3 import cv2
4 # Parameters
5 MODEL_FPATH = "face-detection-retail-0004.bin"
6 ARCH_FPATH = "face-detection-retail-0004.xml"
7 CONF_THRESH = 0.5 # confidence of each object detected
8 
9 # Load the model.
10 net = cv2.dnn.readNet(ARCH_FPATH, MODEL_FPATH)
11 
12 # Specify NCS2 Myriad as a target device.
13 net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
14 net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # DNN_TARGET_CPU (only if you don't have NCS2) or DNN_TARGET_MYRIAD
15 
16 vid_cap = cv2.VideoCapture(0)
17 if not vid_cap.isOpened():
18  raise IOError("Webcam cannot be opened!")
19 
20 while True:
21  # Capture frames
22  ret, frame = vid_cap.read()
23 
24  # Prepare input blob and perform inference
25  blob = cv2.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv2.CV_8U)
26  net.setInput(blob)
27  out = net.forward()
28 
29  # Draw detected faces
30  for detect in out.reshape(-1, 7):
31  conf = float(detect[2])
32  xmin = int(detect[3] * frame.shape[1])
33  ymin = int(detect[4] * frame.shape[0])
34  xmax = int(detect[5] * frame.shape[1])
35  ymax = int(detect[6] * frame.shape[0])
36 
37  if conf > CONF_THRESH:
38  cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0), thickness=2)
39 
40  cv2.imshow('Input', frame)
41 
42  # Press "ESC" key to stop webcam
43  if cv2.waitKey(1) == 27:
44  break
45 
46 # Release video capture object and close the window
47 vid_cap.release()
48 cv2.destroyAllWindows()
49 cv2.waitKey(1)