-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputerVisionTest.py
More file actions
281 lines (230 loc) · 8.52 KB
/
computerVisionTest.py
File metadata and controls
281 lines (230 loc) · 8.52 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from cv2 import *
from cv2 import VideoCapture, rotate, ROTATE_90_COUNTERCLOCKWISE
import pygame
from pygame.locals import *
from random import randint
"""
This file is a little computer vision experiment I worked on in early 2019
The file features two classes: the camera and the sensor
The program uses your computer's webcam if available, and will crash
if you do not have one
The camera detects changes in its environment, and can mark target locations
as items with the most movement. This is easily seen if moving your head while
the program is running. The sensor is more accurate if the webcam is still
You can tinker with the drawn output on the screen by using drawOnScreen, drawVisionOnScreen, blitTarget and blitTargets available in the Sensor class
The program is not built for performance, and is not be suitable for real-time applications
@Author Harrison Boyle-Thomas
@Date: ~2019
"""
pygame.init()
window = pygame.display.set_mode((640, 480))
#cam = VideoCapture(0)
#s, img = cam.read()
"""
#surface = img
while True:
s, img = cam.read()
if s:
a = 0
surface = pygame.surfarray.make_surface(img)
#for y in range(0, len(img)-1):
# for x in range(0, len(img[y])-1):
# a = a + 1
# surface.set_at((x,y), img[y][x])
destroyWindow("cam-test")
window.blit(surface, (0,0))
pygame.display.update()
pygame.event.get()
"""
"""
Camera class that holds information about frame differences seen by the device's camera
"""
class Camera():
"""
previousImage = image from the last cycle
currentImage = image from the current cycle
cam = the device's camera
comparisonImage = the image used to detect changes between prev and cur images
locations = list of key change areas on the comparisonImage
"""
def __init__ (self):
self.previousImage = None
self.currentImage = None
self.cam = VideoCapture(0)
self.comparisonImage = None
self.locations = []
"""
updates the prev/cur images using the device's camera
returns the currentImage
"""
def getNextImage(self):
self.clearLocations()
successful, image = self.cam.read()
if(successful):
image = rotate(image, ROTATE_90_COUNTERCLOCKWISE)
surface = pygame.surfarray.make_surface(image)
if self.currentImage == None:
self.currentImage = surface
self.previousImage = self.currentImage
self.currentImage = surface
self.setComparisonImage()
return surface
return None
"""
resets the locations list
"""
def clearLocations(self):
self.locations = []
"""
creates a comparionImage by subtracting the new image from the old image
this new image is monochrome- whiter = more change, darker = less change
returns the comparison image
"""
def setComparisonImage(self):
if self.previousImage == None or self.currentImage == None:
return None
surface = self.previousImage.copy()
#surface = pygame.surface.Surface((640,480)).convert()
#surface.blit(self.previousImage, (0,0))
surface.blit(self.currentImage, (0,0), special_flags = pygame.BLEND_RGBA_SUB)
self.comparisonImage = surface
return surface
"""
return the comparisonImage
"""
def getComparisonImage(self):
return self.comparisonImage
"""
using the comparison image, the camera looks for pixels with a colour value greater than the threshold,
and marks these locations as possible targets
@param threshold = average colour the pixel must be greater than to be marked
returns the target locations
"""
def getTargetLocations(self, threshold):
if len(self.locations) > 0:
return self.locations
#locations = []
#surface = self.getComparisonImage()
#if surface == None:
# return locations
#for x in range(0, surface.get_width()):
# for y in range(0, surface.get_height()):
# colour = surface.get_at((x,y))
# colourSum = colour[0] + colour[1] + colour[2]
# if colourSum/3 >= threshold:
# locations.append((x,y))
#self.locations = locations
#This new method is slightly faster than the older method, and produces the same results
if(self.getComparisonImage() == None):
return []
array = pygame.PixelArray(self.getComparisonImage())
locations = []
for x in range(0, self.getComparisonImage().get_width()):
for y in range(0, self.getComparisonImage().get_height()):
colour = pygame.Color(array[x,y])
colourSum = (colour[0] + colour[1] + colour[2])/3
if colourSum >= threshold:
locations.append((x,y))
self.locations = locations
return locations
"""
draws a mark at the given locations
@param locations = list of target locations(x,y) on screen
"""
def blitLocations(self, locations):
mark = pygame.surface.Surface((5,5))
mark.fill((255,0,0))
for loc in locations:
window.blit(mark, (loc[0]-2, loc[1]-2))
"""
returns the current cycle image
"""
def getCurrentImage(self):
return self.currentImage
"""
Sensor class that holds information about targets
"""
class Sensor():
"""Contains a camera, current target, and sensitivity
@param colourThreshold = sensitivity
"""
def __init__ (self, colourThreshold):
self.cam = Camera()
self.target = None
self.setSensitivity(colourThreshold)
"""
sets the sensitivity for threat detection
higher value = less sensive
valuable range = 0-255
"""
def setSensitivity(self, threshold):
self.sensitivity = threshold
if self.sensitivity > 255:
self.sensitivity = 255
if self.sensitivity < 0:
self.sensitivity = 0
return self.sensitivity
"""
finds the first available target detected by the camera
"""
def findTarget(self):
#self.cam.getNextImage()
locations = self.cam.getTargetLocations(self.sensitivity)
if len(locations) > 0:
self.target = locations[randint(0, len(locations)-1)]#locations[0]
return locations
"""
draws a red blip to show where the current target is in the image
"""
def blitTarget(self, window):
if self.target != None:
self.cam.blitLocations([self.target])
"""
draws all possible targets on the screen
"""
def blitTargets(self, window):
self.cam.blitLocations(self.cam.getTargetLocations(self.sensitivity))
"""
returns the camera of the Sensor
"""
def getCamera(self):
return self.cam
"""
updates the camera image slots(steps the recording forward
"""
def updateCameraImages(self):
self.cam.getNextImage()
"""
returns the current target
"""
def getTarget(self):
return self.target
"""
draws the current camera view on screen
"""
def drawOnScreen(self, window):
image = self.cam.getCurrentImage()
if(image != None):
window.blit(image, (0,0))
"""
draws the Sensor's "vision"- a monochrome image
with whiter = biggest change between frames
"""
def drawVisionOnScreen(self, window):
window.blit(self.cam.getComparisonImage(), (0,0))
sensor = Sensor(30)
cam = Camera()
while True:
#cam.getNextImage()
sensor.updateCameraImages()
#DRAW VISION
sensor.drawOnScreen(window) #Draw the video feed
#sensor.drawVisionOnScreen(window) #Draw a monochrome image of what the sensor can see
sensor.findTarget()
#DRAW TARGETS
sensor.blitTarget(window) #Draw the most likely target in red
#sensor.blitTargets(window) #Draw all detected targets in red
#window.blit(cam.getCurrentImage(), (0,0))
#cam.blitLocations(cam.getTargetLocations(200))
pygame.display.update()
pygame.event.get()