forked from harishpmiitgn/robotics-me639-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
72 lines (48 loc) · 1.9 KB
/
render.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
import cv2
import numpy
import matplotlib.pyplot as plt
import os
def scaleAndShow(im, name = 'window', height = None, waitKey = 1):
def callback(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print(x, y, im[y, x])
cv2.namedWindow(name)
cv2.setMouseCallback(name,callback)
if height is not None:
width = int(im.shape[1]*height/im.shape[0])
im = cv2.resize(im, (width, height), interpolation= cv2.INTER_NEAREST)
cv2.imshow(name, im)
if cv2.waitKey(waitKey) == ord('q'):
exit()
class Renderer():
def __init__(self, height = 600, width = 600, recordLocation = None ):
shape = (height, width, 3)
self.height = height
self.width = width
self.writer = None
if recordLocation is not None:
self.writer = cv2.VideoWriter(recordLocation, cv2.VideoWriter_fourcc(*'XVID'), 25, (width, height))
self.origImage = numpy.ones(shape, dtype=numpy.uint8) * 255
def putText(self, image, info = {}):
for i, (k, v) in enumerate(info.items()):
cv2.putText(image, k + ' : ' + str(v), (10, 20 + i*20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1, cv2.LINE_AA)
return image
def draw(self, image):
raise NotImplementedError
def getInfo(self):
raise NotImplementedError
def render(self, height = 600, pause = 10):
image = self.origImage.copy()
image = self.draw(image)
image = self.putText(image, self.getInfo())
if self.writer is not None:
self.writer.write(image)
scaleAndShow(image, height= height, waitKey = pause)
return image
if __name__ == "__main__":
def render(image):
cv2.line(image, (0, 0), (100, 100), (0, 128, 0), 1)
return image
r = Renderer(600, 600, render)
for i in range(100):
r.render(info = {'hello' : '00'})