-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
76 lines (51 loc) · 2.15 KB
/
index.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
import cv2
import serial
import time
from PIL import Image
ser = serial.Serial('COM5', 115200) # Replace 'COM5' by the "COM" thing ur arduino is using
time.sleep(2)
video_path = "bad_apple.mp4" # video path
block_size = 8
width = 128
height = 64
def send_block(x, y, block_data):
"""sends data"""
ser.write(bytearray([x, y]))
ser.write(bytearray(block_data))
time.sleep(0.005)
def compare_and_send(image1, image2):
"""Compare images"""
for y in range(0, height, block_size):
for x in range(0, width, block_size):
block1 = [image1.getpixel((x + j, y + i)) for i in range(block_size) for j in range(block_size)]
block2 = [image2.getpixel((x + j, y + i)) for i in range(block_size) for j in range(block_size)]
if block1 != block2:
block_data = []
for i in range(block_size):
byte = 0
for j in range(block_size):
if block2[i * block_size + j] == 0: # black = 1, white = 0
byte |= (1 << (block_size - 1 - j))
block_data.append(byte)
send_block(x, y, block_data)
def process_video(video_path):
"""frame processing"""
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error : can't read the video... maybe you don't have a bad apple ? ")
return
ret, frame = cap.read()
previous_image = None
while ret:
frame_resized = cv2.resize(frame, (width, height), interpolation=cv2.INTER_AREA)
frame_gray = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2GRAY) # processing the b&w thing for no color bugs or type of strenge thing
_, frame_bw = cv2.threshold(frame_gray, 128, 255, cv2.THRESH_BINARY)
current_image = Image.fromarray(frame_bw).convert('1')
if previous_image is not None:
compare_and_send(previous_image, current_image)
previous_image = current_image
ret, frame = cap.read()
time.sleep(0.04)
cap.release()
process_video(video_path)
ser.close()