-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_operations.py
91 lines (67 loc) · 2.36 KB
/
image_operations.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
from PIL import Image
import os
import cv2
import time
should_stream = False
latest_frame = None
def start_stream():
global should_stream
global latest_frame
should_stream = True
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while should_stream:
success, frame = camera.read()
latest_frame = frame
if latest_frame is not None:
cv2.imshow("Live stream", latest_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# release video capture
camera.release()
cv2.destroyAllWindows()
def stop_stream():
global should_stream
should_stream = False
def get_frame():
global latest_frame
return latest_frame
def palettize(filename):
limitedColours = Image.open(filename).quantize(colors=64)
bmp_filename = 'Images/limited.bmp'
limitedColours.save(bmp_filename)
palette_filename = getPalette(bmp_filename)
return bmp_filename, palette_filename
def getPalette(filename):
palette_filename = 'Images/limited.txt'
with open(filename, "rb") as f:
f.read(54) # go to offset 54
palette_bytes = f.read(256)
paletteFile = open(palette_filename, 'w')
byte_iter = iter(palette_bytes)
for byte in byte_iter:
b = byte
g = next(byte_iter)
r = next(byte_iter)
paletteFile.write(rgb2hex(r,g,b) + '\n')
# force iterator to next 0 byte
try:
next(byte_iter)
except:
break
return palette_filename
def rgb2hex(r, g, b):
return '#{:02x}{:02x}{:02x}'.format(r,g,b)
def boundingBoxes(frame, objects, translations):
textheight = 0
for i in range(0, len(objects)):
coords = objects[i]['rectangle']
x, y = coords['x'], coords['y']
w, h = coords['w'], coords['h']
cv2.rectangle(frame, (x,y), (x + w, y + h), (0,255,0), 2)
textheight = y - 10 if (y + h + 10) > 480 else y + h + 10
cv2.putText(frame, translations[i], (x, textheight), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,25,220), 2)
filename = 'Images/boxed.bmp'
cv2.imwrite(filename, frame)
return filename