Prateek Pant
Published © MIT

Auto Focus of Detected Object From a Live Stream

In this project objects like cars and person get detected through a live video stream using a webcam and the background is made blurred.

IntermediateFull instructions provided4 hours705
Auto Focus of Detected Object From a Live Stream

Things used in this project

Story

Read more

Schematics

setup

Logitech webcam and Movidius NCS connected to Raspberry PI B+

Code

Background Blur

Python
Blurs the background and background is alpha blended with the detected object .
  # get cordinates of bounding box
        (y1, x1) = output_dict.get('detection_boxes_' + str(i))[0]
        (y2, x2) = output_dict.get('detection_boxes_' + str(i))[1]

        if detection == True:
          height=y2-y1
          width=x2-x1
          mask = np.zeros((480,640),np.uint8)
          cv2.rectangle(mask,(x1,y1),(x1+width,y1+height),(255,255,255),-1)
          mask = cv2.GaussianBlur(mask, (37,37), 0 )
          blurred = cv2.GaussianBlur(frame, (37,37), 0) 
          alpha = cv2.cvtColor(255-mask, cv2.COLOR_GRAY2BGR)/255.0
          frame = cv2.convertScaleAbs(frame*(1-alpha) + blurred*alpha)
          detection=False
       
    # If a display is available, show the image on which inference was performed
    if 'DISPLAY' in os.environ:
        cv2.imshow( 'Auto Focus', frame )

Object blur

Python
In this case object is blurred but background is unaffected .
for i in range( 0, output_dict['num_detections'] ):
        if( (labels[ int(output_dict['detection_classes_' + str(i)]) ]=="15: person") or  (labels[ int(output_dict['detection_classes_' + str(i)]) ]=="7: car" ) ):
           detection=True

        # get cordinates of bounding box
        (y1, x1) = output_dict.get('detection_boxes_' + str(i))[0]
        (y2, x2) = output_dict.get('detection_boxes_' + str(i))[1]

        if detection == True:
          height=y2-y1
          width=x2-x1
          mask = np.zeros((480,640),np.uint8)
          cv2.rectangle(mask,(x1,y1),(x1+width,y1+height),(255,255,255),-1)
          mask = cv2.GaussianBlur(mask, (37,37), 0 )
          blurred = cv2.GaussianBlur(frame, (37,37), 0) 
          alpha = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)/255.0
          frame = cv2.convertScaleAbs(frame*(1-alpha) + blurred*alpha)
          detection=False

Credits

Prateek Pant

Prateek Pant

2 projects • 3 followers
Video Enthusiast

Comments