import numpy as np
from multiprocessing import Process, Value, sharedctypes
import ctypes
import cv2
class Camera_p(Process):
def __init__(self, arr, isGrab):
super(Camera_p, self).__init__()
self.arr = arr
self.isGrab = isGrab
def run(self):
cap = cv2.VideoCapture(0)
while self.isGrab.value:
_, frame = cap.read()
frame = frame.ravel()
temp = np.frombuffer(self.arr, dtype=np.uint8)
temp[:] = frame
class ShowWindow:
def __init__(self):
self.camera_arr = sharedctypes.RawArray(ctypes.c_uint8, 640 * 480 * 3)
self.isGrab = Value('i', True)
def show_pic(self):
cam_pro = Camera_p(self.camera_arr, self.isGrab)
cam_pro.start()
while True:
frame_show = np.frombuffer(self.camera_arr, dtype=np.uint8).reshape(480, 640, 3)
cv2.imshow('Camera Feed', frame_show)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
self.isGrab.value = False
break
cam_pro.join()
def main():
show = ShowWindow()
show.show_pic()
if __name__ == '__main__':
main()