Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import math
- import socket
- import time
- UDP_IP = "127.0.0.1"
- UDP_PORT = 5065
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- last = (0,0)
- # Open Camera
- try:
- default = 0 # Try Changing it to 1 if webcam not found
- capture = cv2.VideoCapture(default)
- except:
- print("No Camera Source Found!")
- while capture.isOpened():
- # Capture frames from the camera
- ret, frame = capture.read()
- # Get hand data from the rectangle sub window
- #cv2.rectangle(frame,(100,100),(300,300),(0,255,0),0)
- crop_image = frame[100:500, 100:500]
- # Apply Gaussian blur
- blur = cv2.GaussianBlur(crop_image, (3,3), 0)
- # Change color-space from BGR -> HSV
- hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
- # lower mask (0-10)
- lower_red = np.array([0,50,50])
- upper_red = np.array([10,255,255])
- mask0 = cv2.inRange(hsv, lower_red, upper_red)
- # upper mask (170-180)
- lower_red = np.array([100,50,50])
- upper_red = np.array([140,255,255])
- mask1 = cv2.inRange(hsv, lower_red, upper_red)
- # join my masks
- mask2 = mask0+mask1
- # Create a binary image with where white will be skin colors and rest is black
- #mask2 = cv2.inRange(hsv, np.array([2,0,0]), np.array([20,255,255]))
- # Kernel for morphological transformation
- kernel = np.ones((5,5))
- # Apply morphological transformations to filter out the background noise
- dilation = cv2.dilate(mask1, kernel, iterations = 1)
- erosion = cv2.erode(dilation, kernel, iterations = 1)
- # Apply Gaussian Blur and Threshold
- filtered = cv2.GaussianBlur(erosion, (3,3), 0)
- ret,thresh = cv2.threshold(filtered, 127, 255, 0)
- # Show threshold image
- # cv2.imshow("Thresholded", thresh)
- # Find contours
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
- try:
- # Find contour with maximum area
- contour = max(contours, key = lambda x: cv2.contourArea(x))
- # Create bounding rectangle around the contour
- x,y,w,h = cv2.boundingRect(contour)
- cv2.rectangle(crop_image,(x,y),(x+w,y+h),(0,0,255),0)
- # Find convex hull
- hull = cv2.convexHull(contour)
- # Draw contour
- drawing = np.zeros(crop_image.shape, np.uint8)
- cv2.drawContours(drawing,[contour],-1,(0,255,0),0)
- cv2.drawContours(drawing,[hull],-1,(0,0,255),0)
- # Show required images
- cv2.imshow("Full Frame", frame)
- all_image = np.hstack((drawing, crop_image))
- cv2.imshow('Recognition', all_image)
- cv2.imshow("Threshold Binary", thresh)
- # cv2.imshow("Filtered IMG 0", mask0)
- cv2.imshow("Filtered IMG 1", mask1)
- pos = ((x+w)/2,(y+h)/2)
- if pos != last:
- info = str(pos[0]) + "," +str(pos[1])
- sock.sendto( (info).encode(), (UDP_IP, UDP_PORT) )
- print("_"*10, "Curr Pos: "+ info, "_"*10)
- last = ((x+w)/2,(y+h)/2);
- time.sleep(0.04);
- except:
- pass
- # Close the camera if 'q' is pressed
- if cv2.waitKey(1) == ord('q'):
- break
- capture.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement