Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Object Detection Realtime
- import numpy as np
- import cv2
- blob_params = cv2.SimpleBlobDetector_Params()
- blob_params.minThreshold = 10
- blob_params.maxThreshold = 100
- blob_params.filterByArea = True
- blob_params.minArea = 1500
- blob_params.filterByCircularity = True
- blob_params.minCircularity = 0.1
- blob_params.filterByConvexity = True
- blob_params.minConvexity = 0.87
- blob_params.filterByInertia = True
- blob_params.minInertiaRatio = 0.01
- # Set this to 255 to detect bright objects
- # blob_params.blobColor = 255
- blob_detector = cv2.SimpleBlobDetector_create(blob_params)
- body_cascade = cv2.CascadeClassifier('haarcascade_upperbody.xml')
- if body_cascade.empty():
- body_cascade = cv2.CascadeClassifier('haarcascade_fullbody.xml')
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
- body_loaded = body_cascade and not body_cascade.empty()
- face_loaded = face_cascade and not face_cascade.empty()
- eye_loaded = eye_cascade and not eye_cascade.empty()
- cascades_loaded = body_loaded and face_loaded and eye_loaded
- cap = cv2.VideoCapture(1)
- if not cap.isOpened():
- cap = cv2.VideoCapture(0)
- if not cap.isOpened():
- exit(1)
- cap.set(3,640) # set Width
- cap.set(4,480) # set Height
- while(True):
- ret, img = cap.read()
- #frame = cv2.flip(frame, -1) # Flip camera vertically
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- blobs = blob_detector.detect(img)
- cv2.drawKeypoints(img, blobs, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
- for kp in blobs:
- cv2.circle(img, (int(kp.pt[0]), int(kp.pt[1])), int(kp.size), (0, 0, 255))
- if cascades_loaded:
- bodies = body_cascade.detectMultiScale(gray, 1.3, 5)
- for (x,y,w,h) in bodies:
- cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2)
- print("body")
- faces = face_cascade.detectMultiScale(gray, 1.3, 5)
- for (x,y,w,h) in faces:
- cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
- print("face")
- roi_gray = gray[y:y+h, x:x+w]
- roi_color = img[y:y+h, x:x+w]
- eyes = eye_cascade.detectMultiScale(roi_gray)
- for(ex,ey,ew,eh) in eyes:
- cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,255,0),2)
- print("eye")
- cv2.imshow('frame', img)
- cv2.imshow('gray', gray)
- k = cv2.waitKey(30) & 0xff
- if k == 27: # press 'ESC' to quit
- break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement