SHARE
TWEET

Untitled

a guest Aug 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # load HL detection model from imageAI
  2. # open camera with openCV, analyze frame by frame
  3. # draw a red frame around the detected object
  4.  
  5. from imageai.Detection.Custom import CustomObjectDetection
  6. import os
  7. import cv2
  8.  
  9. detector = CustomObjectDetection()
  10. detector.setModelTypeAsYOLOv3()
  11. detector.setModelPath("hololens-ex-60--loss-2.76.h5")
  12. detector.setJsonPath("detection_config.json")
  13. detector.loadModel()
  14.  
  15. # init camera
  16. execution_path = os.getcwd()
  17. camera = cv2.VideoCapture(0)
  18.  
  19. while True:
  20.  
  21.     # Grab a single frame of video
  22.     ret, frame = camera.read()
  23.  
  24.     detected_image, detections = detector.detectObjectsFromImage(input_image=frame, input_type="array", output_type="array")
  25.  
  26.     for detection in detections:
  27.         print(detection["name"], " : ", detection["percentage_probability"])
  28.         (x1, y1, x2, y2) = detection["box_points"]
  29.         print("x1: ", x1, " - y1: ", y1, " - x2: ", x2, " - y2: ", y2)
  30.    
  31.         # frame for the detected object
  32.         cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
  33.  
  34.         # Draw a label with the detected object type below the frame
  35.         font = cv2.FONT_HERSHEY_DUPLEX
  36.         cv2.putText(frame, detection["name"], (x1 + 6, y1 - 6), font, 1.0, (255, 255, 255), 1)
  37.  
  38.     # Display the resulting image
  39.     cv2.imshow('Video', frame)
  40.  
  41.     # Hit 'q' on the keyboard to quit!
  42.     if cv2.waitKey(1) & 0xFF == ord('q'):
  43.         break
  44.  
  45. # Release handle to the webcam
  46. video_capture.release()
  47. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top