Skip to content

Commit f308862

Browse files
authored
signal processing and video capture modules added
1 parent 12907a4 commit f308862

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

signalprocessing.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import cv2
2+
import numpy as np
3+
4+
class Detection(object):
5+
6+
THRESHOLD = 1500
7+
8+
def __init__(self, image):
9+
10+
#converting input color to black and white for easier processing
11+
self.previous_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
12+
13+
def active_div(self, image):
14+
15+
#obtain a new input image array
16+
current_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
17+
18+
#find difference between previous and current image arrays
19+
delta = cv2.absdiff(self.previous_gray, current_gray)
20+
21+
#any pixels with value greater than 25 is replaced by 255
22+
threshold_image = cv2.threshold(delta, 25, 255, cv2.THRESH_BINARY)[1]
23+
24+
cv2.imshow('OpenCV Detection', image)
25+
cv2.waitKey(10)
26+
27+
#storing new image
28+
self.previous_gray = current_gray
29+
30+
# set cell width
31+
h, w = threshold_image.shape[:2]
32+
cell_width = w//7
33+
34+
#storing the range in which the delta value exists (according to image divisions and octaves)
35+
cells = np.array([0, 0, 0, 0, 0, 0, 0])
36+
#splitting the threshold image previously obtained into 7 divisions
37+
#the countNonZero function returns the number of non-zero elements in division
38+
cells[0] = cv2.countNonZero(threshold_image[0:h, 0:cell_width])
39+
cells[1] = cv2.countNonZero(threshold_image[0:h, cell_width:cell_width*2])
40+
cells[2] = cv2.countNonZero(threshold_image[0:h, cell_width*2:cell_width*3])
41+
cells[3] = cv2.countNonZero(threshold_image[0:h, cell_width*3:cell_width*4])
42+
cells[4] = cv2.countNonZero(threshold_image[0:h, cell_width*4:cell_width*5])
43+
cells[5] = cv2.countNonZero(threshold_image[0:h, cell_width*5:cell_width*6])
44+
cells[6] = cv2.countNonZero(threshold_image[0:h, cell_width*6:w])
45+
46+
# obtaining the most active range
47+
to_press = np.argmax(cells)
48+
49+
# return the most active cell, if threshold met
50+
if(cells[to_press] >= self.THRESHOLD):
51+
return to_press
52+
else:
53+
return None

vidcapture.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import cv2
2+
from threading import Thread
3+
4+
class Webcam:
5+
6+
def __init__(self):
7+
8+
#starting cv2's video capture functionality
9+
#without the cv2.cap_dshow attribute, the program does not function
10+
self.video_capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)
11+
12+
#capturing the video frame as an image for later processing
13+
self.current_frame = self.video_capture.read()[1]
14+
15+
def capture_start(self):
16+
17+
#creating multiprocessing thread to capture frames and send midi signals at same time
18+
p=Thread(target=self._update_frame, args=())
19+
p.start()
20+
21+
22+
def _update_frame(self):
23+
#updating the image that is being processed as time passes (for testing purposes)
24+
while(True):
25+
self.current_frame = self.video_capture.read()[1]
26+
27+
28+
def refresh_image(self):
29+
#Obtaining the current frame
30+
return self.current_frame

0 commit comments

Comments
 (0)