Advertisement
mechagical

Python Color Reco to Unity

Jun 25th, 2022
900
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None
  1. import numpy as np
  2. import cv2
  3. import math
  4. import socket
  5. import time
  6.  
  7. UDP_IP = "127.0.0.1"
  8. UDP_PORT = 5065
  9.  
  10. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  11.  
  12. last = (0,0)
  13.  
  14. # Open Camera
  15. try:
  16.     default = 0 # Try Changing it to 1 if webcam not found
  17.     capture = cv2.VideoCapture(default)
  18. except:
  19.     print("No Camera Source Found!")
  20.  
  21. while capture.isOpened():
  22.    
  23.     # Capture frames from the camera
  24.     ret, frame = capture.read()
  25.    
  26.     # Get hand data from the rectangle sub window  
  27.     #cv2.rectangle(frame,(100,100),(300,300),(0,255,0),0)
  28.     crop_image = frame[100:500, 100:500]
  29.    
  30.     # Apply Gaussian blur
  31.     blur = cv2.GaussianBlur(crop_image, (3,3), 0)
  32.    
  33.     # Change color-space from BGR -> HSV
  34.     hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
  35.  
  36.     # lower mask (0-10)
  37.     lower_red = np.array([0,50,50])
  38.     upper_red = np.array([10,255,255])
  39.     mask0 = cv2.inRange(hsv, lower_red, upper_red)
  40.  
  41.     # upper mask (170-180)
  42.     lower_red = np.array([100,50,50])
  43.     upper_red = np.array([140,255,255])
  44.     mask1 = cv2.inRange(hsv, lower_red, upper_red)
  45.  
  46.     # join my masks
  47.     mask2 = mask0+mask1
  48.  
  49.     # Create a binary image with where white will be skin colors and rest is black
  50.     #mask2 = cv2.inRange(hsv, np.array([2,0,0]), np.array([20,255,255]))
  51.    
  52.     # Kernel for morphological transformation    
  53.     kernel = np.ones((5,5))
  54.    
  55.     # Apply morphological transformations to filter out the background noise
  56.     dilation = cv2.dilate(mask1, kernel, iterations = 1)
  57.     erosion = cv2.erode(dilation, kernel, iterations = 1)    
  58.        
  59.     # Apply Gaussian Blur and Threshold
  60.     filtered = cv2.GaussianBlur(erosion, (3,3), 0)
  61.     ret,thresh = cv2.threshold(filtered, 127, 255, 0)
  62.    
  63.     # Show threshold image
  64.     # cv2.imshow("Thresholded", thresh)
  65.  
  66.     # Find contours
  67.     contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
  68.    
  69.     try:
  70.         # Find contour with maximum area
  71.         contour = max(contours, key = lambda x: cv2.contourArea(x))
  72.        
  73.         # Create bounding rectangle around the contour
  74.         x,y,w,h = cv2.boundingRect(contour)
  75.         cv2.rectangle(crop_image,(x,y),(x+w,y+h),(0,0,255),0)
  76.        
  77.         # Find convex hull
  78.         hull = cv2.convexHull(contour)
  79.        
  80.         # Draw contour
  81.         drawing = np.zeros(crop_image.shape, np.uint8)
  82.         cv2.drawContours(drawing,[contour],-1,(0,255,0),0)
  83.         cv2.drawContours(drawing,[hull],-1,(0,0,255),0)
  84.  
  85.         # Show required images
  86.         cv2.imshow("Full Frame", frame)
  87.         all_image = np.hstack((drawing, crop_image))
  88.         cv2.imshow('Recognition', all_image)
  89.         cv2.imshow("Threshold Binary", thresh)
  90.         # cv2.imshow("Filtered IMG 0", mask0)
  91.         cv2.imshow("Filtered IMG 1", mask1)
  92.  
  93.         pos = ((x+w)/2,(y+h)/2)
  94.         if pos != last:
  95.             info = str(pos[0]) + "," +str(pos[1])
  96.             sock.sendto( (info).encode(), (UDP_IP, UDP_PORT) )
  97.             print("_"*10, "Curr Pos: "+ info, "_"*10)
  98.             last = ((x+w)/2,(y+h)/2);
  99.             time.sleep(0.04);
  100.  
  101.  
  102.     except:
  103.         pass
  104.  
  105.     # Close the camera if 'q' is pressed
  106.     if cv2.waitKey(1) == ord('q'):
  107.         break
  108.  
  109. capture.release()
  110. cv2.destroyAllWindows()
Advertisement
RAW Paste Data Copied
Advertisement