-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraViewer_py.py
executable file
·102 lines (82 loc) · 2.86 KB
/
CameraViewer_py.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
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- Python -*-
"""
CameraViewer_py.py
Kenya Ukai
2015/9/25
Lisence : MIT
"""
import sys
import time
import RTC
import OpenRTM_aist
import cv2
import cv2.cv as cv
import numpy
import time
cameraviewer_py_spec = ["implementation_id", "CameraViewer_py",
"type_name", "CameraViewer_py",
"description", "CameraViewer written in python",
"version", "1.0.0",
"vendor", "Kenya Ukai",
"category", "basic tool",
"activity_type", "STATIC",
"max_instance", "1",
"language", "Python",
"lang_type", "SCRIPT",
""]
class CameraViewer_py(OpenRTM_aist.DataFlowComponentBase):
def __init__(self, manager):
OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
self._d_camera = RTC.CameraImage(RTC.Time(0,0),0, 0, 0, "", 0.0, [])
self._cameraIn = OpenRTM_aist.InPort("camera", self._d_camera)
self.is_alive = True
def onInitialize(self):
self.addInPort("camera",self._cameraIn)
return RTC.RTC_OK
def onFinalize(self):
self.is_alive = False
return RTC.RTC_OK
def onActivated(self, ec_id):
return RTC.RTC_OK
def onDeactivated(self, ec_id):
return RTC.RTC_OK
def onExecute(self, ec_id):
if self._cameraIn.isNew():
self._d_camera = self._cameraIn.read()
return RTC.RTC_OK
def getImage(self):
return self._d_camera
def isAlive(self):
return self.is_alive
def CameraViewer_pyInit(manager):
profile = OpenRTM_aist.Properties(defaults_str=cameraviewer_py_spec)
manager.registerFactory(profile,
CameraViewer_py,
OpenRTM_aist.Delete)
def main():
manager = OpenRTM_aist.Manager.init(sys.argv)
manager.activateManager()
CameraViewer_pyInit(manager)
comp = manager.createComponent("CameraViewer_py")
manager.runManager(True)
print("Press 'esc' on camera window to end.")
cv2.namedWindow("CameraViewer_py", cv.CV_WINDOW_AUTOSIZE)
time1 = 0.0
while comp.isAlive():
time2 = time.time()
fps = 1.0 / (time2 - time1)
time1 = time2
cam_img = comp.getImage()
if cam_img.height>0 and cam_img.width>0:
pixels = numpy.array([ord(i) for i in cam_img.pixels], dtype = numpy.uint8)
pixels = pixels.reshape(cam_img.height, cam_img.width, 3)
cv2.putText(pixels,"END:ESC | %d FPS"%fps,(20,20),cv2.FONT_HERSHEY_PLAIN ,1,(255,255,255))
cv2.imshow("CameraViewer_py",pixels)
if cv2.waitKey(10) == 27:
break
cv2.destroyAllWindows()
manager.terminate()
if __name__ == "__main__":
main()