-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectRecogTest.py
More file actions
78 lines (70 loc) · 2.4 KB
/
ObjectRecogTest.py
File metadata and controls
78 lines (70 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import cv2
cv2.__version__
import numpy as np
def setUp(resolution):
# Defining a range of the color green
global lowerBound
lowerBound=np.array([33,80,40])
global upperBound
upperBound=np.array([102,255,255])
# Defining screen resolution
global horRes
horRes = resolution[0]
global vertRes
vertRes = resolution[1]
def getCoords():
# Make a VideoCapture object (camera)
cam= cv2.VideoCapture(0)
# Making kernel to delete noise (open = erosion followed by dilation, close is reversed)
# MORPH_OPEN deletes noise outside of the object, MORPH_CLOSE inside of the object)
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
# Main loop
# Get the video data
ret, orImg=cam.read()
# Resize the frame, to have not too many pixels
orImg=cv2.resize(orImg,(horRes,vertRes))
global img
img = cv2.flip(orImg, 1)
# convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask, look for the object in this color range
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
# Delete all the noise in the image
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose # This is our final image with object in black-white (object is white)
im2, conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
coords = []
radius = []
centerCoords = [(-1,-1),(-1,-1)]
#cv2.drawContours(img,conts,-1,(255,0,0),3)
for i in range(len(conts)):
#x,y,w,h=cv2.boundingRect(conts[i])
#cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255), 2)
(x,y),rad = cv2.minEnclosingCircle(conts[i])
center = (int(x),int(y))
coords.append(center)
radius.append(int(rad))
i = 0
if len(radius) > 0:
while i < 2:
center = coords[radius.index(max(radius))]
if center[0] < horRes/2:
centerCoords[0] = center
else:
centerCoords[1] = center
coords.remove(center)
rad = max(radius)
radius.remove(max(radius))
#cv2.circle(img,center,rad,(0,22,0),2)
if i == len(radius):
break
i +=1
print (centerCoords)
setUp([1280,720])
while True:
print('test')
getCoords()
cv2.imshow("cam",img)
cv2.waitKey(10)