This repository was archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource code.py
More file actions
165 lines (153 loc) · 5.05 KB
/
source code.py
File metadata and controls
165 lines (153 loc) · 5.05 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import serial
import time
def vid():
import cv2
cam = cv2.VideoCapture(1)
img_counter = 0
while True:
ret, frame = cam.read()
cv2.imshow("test", frame)
if not ret:
break
k = cv2.waitKey(1)
if k%256 == 32:
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
break
cam.release()
cv2.destroyAllWindows()
def vid2():
import cv2
fram=[]
cam = cv2.VideoCapture(1)
cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
break
fram.append(frame)
if(img_counter > 100):
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, fram[50])
print("{} written!".format(img_name))
break
img_counter += 1
cam.release()
cv2.destroyAllWindows()
def fun():
import cv2
import numpy as np
vid()
img=cv2.imread("opencv_frame_0.png",1)
imw=img
cv2.imshow("cndj",img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
shape = "unidentified"
im = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
ima = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
count = 25
while shape=="unidentified":
count+=25
ret1,th1 = cv2.threshold(ima,count,255,cv2.THRESH_BINARY_INV)
cv2.imshow("bvjb",th1)
cv2.waitKey(500)
cv2.destroyAllWindows()
shape = "unidentified"
im2, c, hierarchy = cv2.findContours(th1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(c)>0:
max=cv2.contourArea(c[0])
index=c[0]
for i in c:
area = cv2.contourArea(i)
if area>max:
max=area
index=i
peri = cv2.arcLength(index, True)
approx = cv2.approxPolyDP(index, 0.04 * peri, True)
cv2.drawContours(imw, index, -1, (0, 255, 0), 2)
cv2.imshow("hhgju", imw)
cv2.waitKey(500)
cv2.destroyAllWindows()
if len(approx) == 3 and (peri*peri/cv2.contourArea(index))>=18 and (peri*peri/cv2.contourArea(index))<=23:
shape = "triangle"
elif len(approx) == 4 and (peri*peri/cv2.contourArea(index))>=14 and (peri*peri/cv2.contourArea(index))<=18:
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
shape = "square" if ar >= 0.90 and ar <= 1.10 else "rectangle"
elif len(approx) > 4 and (peri*peri/cv2.contourArea(index))>=11 and (peri*peri/cv2.contourArea(index))<=14:
shape = "circle"
else:
continue
print(len(approx))
return shape
def fun2():
import cv2
import numpy as np
vid2()
img=cv2.imread("opencv_frame_101.png",1)
img = img[:400,:]
imw=img
cv2.imshow("cndj",img)
cv2.waitKey(500)
cv2.destroyAllWindows()
shape = "unidentified"
im = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
ima = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
count = 25
while shape=="unidentified":
count+=25
ret1,th1 = cv2.threshold(ima,count,255,cv2.THRESH_BINARY_INV)
cv2.imshow("bvjb",th1)
cv2.waitKey(1000)
cv2.destroyAllWindows()
shape = "unidentified"
im2, c, hierarchy = cv2.findContours(th1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(c)>0:
max=cv2.contourArea(c[0])
index=c[0]
for i in c:
area = cv2.contourArea(i)
if area>max:
max=area
index=i
peri = cv2.arcLength(index, True)
approx = cv2.approxPolyDP(index, 0.04 * peri, True)
cv2.drawContours(imw, index, -1, (0, 255, 0), 2)
cv2.imshow("hhgju", imw)
cv2.waitKey(500)
cv2.destroyAllWindows()
if len(approx) == 3 and (peri*peri/cv2.contourArea(index))>=18 and (peri*peri/cv2.contourArea(index))<=23:
shape = "triangle"
elif len(approx) == 4 and (peri*peri/cv2.contourArea(index))>=14 and (peri*peri/cv2.contourArea(index))<=18:
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
shape = "square" if ar >= 0.90 and ar <= 1.10 else "rectangle"
elif len(approx) > 4 and (peri*peri/cv2.contourArea(index))>=11 and (peri*peri/cv2.contourArea(index))<=14:
shape = "circle"
else:
continue
print(len(approx))
return shape
#arduino = serial.Serial("COM5",115200)
#time.sleep(2)
a=fun()
t=0
while t==0:
b=fun2()
print(b)
if b==a:
t-=1
c="f"
#arduino.write(b'f')
else:
c="l"
arduino.write(b'l')
#while True:
#cm=arduino.read()
#if cm=='y':
#break
print(a,b,c)