-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnightshow.py
201 lines (129 loc) · 4.92 KB
/
nightshow.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 11 13:28:32 2017
@author: Durgesh Reddiyar
"""
from tkinter import *
import cv2
import numpy as np
import time
#from tkinter import messagebox
import ctypes
import os
from datetime import datetime
from PIL import Image
from appJar import gui
app = gui()
#temporary area
def callback():
#cascades
boxer_cascade=cv2.CascadeClassifier('/home/pi/Desktop/image_proc/boxer_25.xml')
hero_cascade=cv2.CascadeClassifier('/home/pi/Desktop/image_proc/hero_dee.xml')
#hero_cascade=cv2.CascadeClassifier('C:\\Users\\Durgesh Reddiyar\\Desktop\\sayali\\cascades_part3\\20.xml')
#camera source
cam_source = 0
cap = cv2.VideoCapture(cam_source)
boxer_count=0
hero_count=0
# ti=datetime.datetime.now()
# string_i_want=('%02d'%(ti.second))[:-4]
# print(string_i_want)
termination_time_system1 = datetime.now()
termination_time_start = termination_time_system1.second % 7
print ("start",termination_time_start)
time.sleep(1)
while True:
termination_time_system2 = datetime.now()
termination_time_check = termination_time_system2.second % 7
print ("current",termination_time_check)
select = 0
ret, img = cap.read()
if ret is True:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
boxer = boxer_cascade.detectMultiScale(gray,1.3,5)
hero = hero_cascade.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in boxer:
#cls()
font = cv2.FONT_HERSHEY_SIMPLEX
#cv2.putText(img,'BOXER',(x-w, y-h), font,0.5, (0,255,255), 1,cv2.LINE_AA)
cv2.rectangle(img, (x,y), (x+w, y+h), (255,255,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
print ("boxer_",boxer_count)
boxer_count=boxer_count+1
#select=2
#time.sleep(0.5)
for (x,y,w,h) in hero:
#cls()
font = cv2.FONT_HERSHEY_SIMPLEX
#cv2.putText(img,'HERO',(x-w, y-h), font,0.5, (0,255,255), 1,cv2.LINE_AA)
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,255), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
print ("hero_ ",hero_count)
hero_count=hero_count+1
#select=1
#time.sleep(0.5)
cv2.imshow('Detecting', img)
k = cv2.waitKey(30) & 0xff
if k == 27 or termination_time_start == termination_time_check:
break
ret,img=cap.read()
# cv2.imshow('capture.jpg',img)
cv2.imwrite('/home/pi/Desktop/image_proc/capture.jpg',img)
cap.release()
cv2.destroyAllWindows()
if(boxer_count>hero_count):
print("boxer found")
select=2
elif (boxer_count<hero_count):
print("hero found")
select=1
else:
print("no object detected")
img_rgb = cv2.imread('capture.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template_array2 = ["temp1_1.jpg","temp1_2.jpg"]
if select == 1:
item = template_array2[0]
if select == 2:
item = template_array2[1]
template=cv2.imread(item,2)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.75
loc = np.where( res >= threshold)
counter1 = 0
temp_ptx=999
temp_pty=999
for pt in zip(*loc[::-1]):
im = Image.open("/home/pi/Desktop/image_proc/capture.jpg")
rgb_im = im.convert('RGB')
ptx = int(pt[0] + w - 2)
pty = int(pt[1] + h )
posx = abs(temp_ptx - ptx)
posy = abs(temp_pty - pty)
if posx >5 and posy >5:
print(ptx,pty)
temp_ptx = ptx
temp_pty = pty
r, g, b = rgb_im.getpixel((ptx,pty))
print ("RGB",r, g, b)
if r>200 and g<50 and b<50:
continue
else:
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)
counter1 = counter1 +1
cv2.imwrite('res1.jpg',img_rgb)
print ("first ",counter1)
cv2.waitKey()
if(select==1 and counter1==1):
app.infoBox(title, "hero passed")
if(select==2 and counter1==5):
app.infoBox(title, "boxer passed")
if(select==1 and counter1 != 1):
app.infoBox(title, "fault detected in hero")
if(select==2 and counter1 != 5):
app.infoBox(title, "fault detected in boxer")
callback()
app.go()