-
Notifications
You must be signed in to change notification settings - Fork 48
/
camera_capture_with_opencv.py
65 lines (53 loc) · 2.28 KB
/
camera_capture_with_opencv.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
import dearpygui.dearpygui as dpg
import cv2 as cv
import numpy as np
dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=800)
dpg.setup_dearpygui()
vid = cv.VideoCapture(0)
ret, frame = vid.read()
# image size or you can get this from image shape
frame_width = vid.get(cv.CAP_PROP_FRAME_WIDTH)
frame_height = vid.get(cv.CAP_PROP_FRAME_HEIGHT)
video_fps = vid.get(cv.CAP_PROP_FPS)
print(frame_width)
print(frame_height)
print(video_fps)
print("Frame Array:")
print("Array is of type: ", type(frame))
print("No. of dimensions: ", frame.ndim)
print("Shape of array: ", frame.shape)
print("Size of array: ", frame.size)
print("Array stores elements of type: ", frame.dtype)
data = np.flip(frame, 2) # because the camera data comes in as BGR and we need RGB
data = data.ravel() # flatten camera data to a 1 d stricture
data = np.asfarray(data, dtype='f') # change data type to 32bit floats
texture_data = np.true_divide(data, 255.0) # normalize image data to prepare for GPU
print("texture_data Array:")
print("Array is of type: ", type(texture_data))
print("No. of dimensions: ", texture_data.ndim)
print("Shape of array: ", texture_data.shape)
print("Size of array: ", texture_data.size)
print("Array stores elements of type: ", texture_data.dtype)
with dpg.texture_registry(show=True):
dpg.add_raw_texture(frame.shape[1], frame.shape[0], texture_data, tag="texture_tag", format=dpg.mvFormat_Float_rgb)
with dpg.window(label="Example Window"):
dpg.add_text("Hello, world")
dpg.add_image("texture_tag")
dpg.show_metrics()
dpg.show_viewport()
while dpg.is_dearpygui_running():
# updating the texture in a while loop the frame rate will be limited to the camera frame rate.
# commenting out the "ret, frame = vid.read()" line will show the full speed that operations and updating a texture can run at
ret, frame = vid.read()
data = np.flip(frame, 2)
data = data.ravel()
data = np.asfarray(data, dtype='f')
texture_data = np.true_divide(data, 255.0)
dpg.set_value("texture_tag", texture_data)
# to compare to the base example in the open cv tutorials uncomment below
#cv.imshow('frame', frame)
dpg.render_dearpygui_frame()
vid.release()
#cv.destroyAllWindows() # when using upen cv window "imshow" call this also
dpg.destroy_context()