def start_cameras(): left_camera = CSI_Camera() left_camera.create_gstreamer_pipeline( sensor_id=0, sensor_mode=SENSOR_MODE_720, framerate=30, flip_method=0, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH, ) left_camera.open(left_camera.gstreamer_pipeline) left_camera.start() right_camera = CSI_Camera() right_camera.create_gstreamer_pipeline( sensor_id=1, sensor_mode=SENSOR_MODE_720, framerate=30, flip_method=0, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH, ) right_camera.open(right_camera.gstreamer_pipeline) right_camera.start() cv2.namedWindow("CSI Cameras", cv2.WINDOW_AUTOSIZE) if (not left_camera.video_capture.isOpened() or not right_camera.video_capture.isOpened()): # Cameras did not open, or no camera attached print("Unable to open any cameras") # TODO: Proper Cleanup SystemExit(0) try: # Start counting the number of frames read and displayed left_camera.start_counting_fps() right_camera.start_counting_fps() while cv2.getWindowProperty("CSI Cameras", 0) >= 0: left_image = read_camera(left_camera, show_fps) right_image = read_camera(right_camera, show_fps) # We place both images side by side to show in the window camera_images = np.hstack((left_image, right_image)) cv2.imshow("CSI Cameras", camera_images) left_camera.frames_displayed += 1 right_camera.frames_displayed += 1 # This also acts as a frame limiter # Stop the program on the ESC key if (cv2.waitKey(20) & 0xFF) == 27: break finally: left_camera.stop() left_camera.release() right_camera.stop() right_camera.release() cv2.destroyAllWindows()
def getCapture(cap): # 반복적으로 화면 캡쳐를 얻는 함수 # 로컬에 화면 캡쳐 이미지를 저장함 camera = CSI_Camera() camera.create_gstreamer_pipeline(sensor_id=0, sensor_mode=2, framerate=30, flip_method=0, display_height=720, display_width=1280) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Sticker Solution", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: camera.start_counting_fps() while cv2.getWindowProperty("Sticker Solution", 0) >= 0: _, img = camera.read() cv2.imwrite("images/" + str(cap) + ".jpg", img) cv2.imshow("Sticker Solution", img) camera.frames_displayed += 1 cap = cap + 1 if (cv2.waitKey(5) & 0xFF) == 27: break finally: camera.stop() camera.release() cv2.destroyAllWindows()
def face_detect(): face_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml") eye_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_eye.xml") left_camera = CSI_Camera() left_camera.create_gstreamer_pipeline( sensor_id=0, sensor_mode=SENSOR_MODE_720, framerate=30, flip_method=0, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH, ) left_camera.open(left_camera.gstreamer_pipeline) left_camera.start() cv2.namedWindow("Face Detect", cv2.WINDOW_AUTOSIZE) if (not left_camera.video_capture.isOpened()): # Cameras did not open, or no camera attached print("Unable to open any cameras") # TODO: Proper Cleanup SystemExit(0) try: # Start counting the number of frames read and displayed left_camera.start_counting_fps() while cv2.getWindowProperty("Face Detect", 0) >= 0: img = read_camera(left_camera, False) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) if show_fps: draw_label( img, "Frames Displayed (PS): " + str(left_camera.last_frames_displayed), (10, 20)) draw_label( img, "Frames Read (PS): " + str(left_camera.last_frames_read), (10, 40)) cv2.imshow("Face Detect", img) left_camera.frames_displayed += 1 keyCode = cv2.waitKey(5) & 0xFF # Stop the program on the ESC key if keyCode == 27: break finally: left_camera.stop() left_camera.release() cv2.destroyAllWindows()
def lighter_detect(): camera = CSI_Camera() camera.create_gstreamer_pipeline( sensor_id = 0, sensor_mode = SENSOR_MODE, framerate = 30, flip_method = 0, display_height = DISPLAY_HEIGHT, display_width = DISPLAY_WIDTH, ) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Lighter Test FPS", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: camera.start_counting_fps() while cv2.getWindowProperty("Lighter Test FPS", 0) >= 0: img = read_camera(camera) blob = cv2.dnn.blobFromImage(img, 0.00392, (446, 446), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5 : center_x = int(detection[0] * DISPLAY_WIDTH) center_y = int(detection[1] * DISPLAY_HEIGHT) w = int(detection[2] * DISPLAY_WIDTH) h = int(detection[3] * DISPLAY_HEIGHT) x = int(center_x - w / 2) y = int(center_y - h / 2) if class_id == 0: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2, cv2.LINE_AA) elif class_id == 1: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2, cv2.LINE_AA) else: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2, cv2.LINE_AA) cv2.putText(img, classes[class_id], (x, y+30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) cv2.imshow("Lighter Test FPS", img) camera.frames_displayed += 1 if (cv2.waitKey(5) & 0xFF) == 27: break # ESC key Stops program finally: camera.stop() camera.release() cv2.destroyAllWindows()
def getCapture(): camera = CSI_Camera() camera.create_gstreamer_pipeline(sensor_id=0, sensor_mode=0, framerate=30, flip_method=0, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Gas Test Display", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: camera.start_counting_fps() image_num = 1 gpu_frame = cv2.cuda_GpuMat() while cv2.getWindowProperty("Gas Test Display", 0) >= 0: _, original_img = camera.read() gpu_frame.upload(original_img) # Apply grayscale and sobel filter #temp = cv2.cvtColor(original_img, cv2.COLOR_RGB2GRAY) #img = np.zeros([DISPLAY_HEIGHT, DISPLAY_WIDTH, 3]) #for i in range(3) : img[:, :, i] = temp[:, :] #img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15) #img = cv2.convertScaleAbs(img) # Apply Sharpnening filter #img = getSharpening(img, 2) gpu_frame.download(original_img) cv2.imshow("Gas Test Display", original_img) camera.frames_displayed += 1 if (cv2.waitKey(25) & 0xFF) == 27: break # Quit if you pressed ESC. #if (cv2.waitKey(25) & 0xFF) == 13: # Capture if you pressed ENTER. # cv2.imwrite('/home/jyjun/test' + str(image_num) + '.png', img) # print("<Image captured successfully!>") # image_num = image_num + 1 finally: camera.stop() camera.release() cv2.destroyAllWindows()
def getCapture(cap): # 실시간으로 화면을 캡쳐 후 로컬저장함 camera = CSI_Camera() camera.create_gstreamer_pipeline( sensor_id=0, sensor_mode= 2, # [TODO] Sensor_mode로 설정하지 말고 해상도로 설정하도록 해야할 것 같은데? 4032x3040 (Ratio: 4:3) framerate=30, flip_method=0, display_height=720, # [TODO] 이것도 4:3으로 맞춰주는게 가장 best인데... display_width=1280) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Gas Solution", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: camera.start_counting_fps() while cv2.getWindowProperty("Gas Solution", 0) >= 0: _, img_ori = camera.read() temp = cv2.cvtColor(img_ori, cv2.COLOR_RGB2GRAY) img = np.zeros([720, 720, 3]) for i in range(3): img[:, :, i] = temp[:, 280:1000] img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15) img = cv2.convertScaleAbs(img) cv2.imwrite( "images/" + str(cap) + ".jpg", img) # [중요!] 이미지를 로컬에 저장함! TODO. 이거 저장하지말고 바로 판단하도록 바꿔야함 #cv2.imshow("Gas Solution", img) # [TODO] 이걸 굳이 띄울 필요가 있나? time.sleep(0.33) # 의도적으로 3프레임으로 만들려고 0.33초 sleep camera.frames_displayed += 1 cap = cap + 1 if (cv2.waitKey(5) & 0xFF) == 27: break finally: camera.stop() camera.release() cv2.destroyAllWindows()
def getCapture(): # 반복적으로 화면 캡쳐를 얻는 함수 # 로컬에 화면 캡쳐 이미지를 저장함 camera = CSI_Camera() camera.create_gstreamer_pipeline(sensor_id=0, sensor_mode=1, framerate=30, flip_method=2, display_height=720, display_width=1280) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Sticker Solution", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: # camera.start_counting_fps() # while cv2.getWindowProperty("Sticker Solution", 0) >= 0: cap = 0 while True: # 0.3 second per once # time.sleep(0.3) _, img = camera.read() img = img[:, 280:1000, :] cv2.imshow("img", img) # 외부 통신 삽입 자리 cap += 1 # cv2.imwrite("NewFile1/"+str(cap)+".jpg", img) # time.sleep(0.1) #시작 전 여기 수정 # camera.frames_displayed += 1 if (cv2.waitKey(5) & 0xFF) == 27: break camera.stop() camera.release() cv2.destroyAllWindows() except: print("?")
def getCapture(): # 실시간으로 화면을 캡쳐 후 로컬저장함 global cap camera = CSI_Camera() camera.create_gstreamer_pipeline(sensor_id=0, sensor_mode=2, framerate=15, flip_method=0, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH) camera.open(camera.gstreamer_pipeline) camera.start() cv2.namedWindow("Gas Solution", cv2.WINDOW_AUTOSIZE) if not camera.video_capture.isOpened(): print("Unable to open camera") SystemExit(0) try: camera.start_counting_fps() while cv2.getWindowProperty("Gas Solution", 0) >= 0: _, img_ori = camera.read() temp = cv2.cvtColor(img_ori, cv2.COLOR_RGB2GRAY) img = np.zeros([DISPLAY_HEIGHT, DISPLAY_HEIGHT, 3]) diff_half = (DISPLAY_WIDTH - DISPLAY_HEIGHT) // 2 for i in range(3): img[:, :, i] = temp[:, diff_half:DISPLAY_WIDTH - diff_half] img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15) img = cv2.convertScaleAbs(img) cv2.imwrite("images/" + str(cap) + ".jpg", img) cap = cap + 1 cv2.imshow("Gas Solution", img) camera.frames_displayed += 1 if (cv2.waitKey(5) & 0xFF) == 27: break finally: camera.stop() camera.release() cv2.destroyAllWindows()
def __init__(self): self.camera = CSI_Camera() self.net = cv2.dnn_DetectionModel("model.cfg", "model.weights") self.gpu_template_img = cv2.cuda_GpuMat(TEMPLATE_IMAGE_ACE) self.gpu_target_img = cv2.cuda_GpuMat() ###### Lighter information ####### self.head_width = 0 self.body_height = 0 self.upper_sticker_bound = 0 self.lower_sticker_bound = 0 self.sticker_poses = [] # Lighter's sticker position for each lighter self.error_sticker_images = [] # Manual camera setting variables, initialize with default size. self.manual_box_x = DISPLAY_WIDTH // 2 self.manual_box_y = DISPLAY_HEIGHT // 2 self.manual_box_width = 150 self.manual_box_height = 300 ###### Image Information ###### self.display_contrast = 30 # Default contrast 110% self.display_brightness = 5 # Default brightness 105% self.initialize_camera() self.initialize_yolo()
def checkHeadRatio(raw, stick) : # 라이터 헤드를 찾기 위한 좌표 추정 함수 return int((stick-raw) * (5/105)) #헤드 간 간격/몸통 길이 def checkStickRatio(raw) : return int(raw * (490/260)) def checkStickerRatio(raw, stick) : return int((stick-raw)*(45/216)), int((stick-raw)*(133/216)) def checkStickerRatio2(raw, stick) : return int((stick-raw)*(134/216)), int((stick-raw)*(175/216)) # 로컬에 화면 캡쳐 이미지를 저장함 camera = CSI_Camera() camera.create_gstreamer_pipeline( sensor_id = 0, sensor_mode = 1, framerate = 30, flip_method = 2, display_height = 720, display_width = 1280 ) camera.open(camera.gstreamer_pipeline) camera.start() # cv2.namedWindow("Sticker Solution", cv2.WINDOW_AUTOSIZE) def getStart():
def __init__(self): # Pi-Camera for jetson nano (CSI_Camera) self.camera = CSI_Camera() self.gpu_frame = cv2.cuda_GpuMat() # 4:3 Resolution (Capture Resolution: 4032 x 3040) self.display_width = 1280 # Display width (not captured width) self.display_height = 960 # Display height (not captured height) self.upper_not_roi = self.display_height // 4 # Default value of ROI self.lower_not_roi = 3 * self.display_height // 4 # Default value of ROI # Load YOLOv4 self.net = cv2.dnn_DetectionModel("yolov4-tiny.cfg", "yolov4-tiny-final.weights") self.net.setInputSize(448, 448) # It can be (416, 416) either self.net.setInputScale(1.0 / 255) # Scaled by 1byte [0, 255] self.net.setInputSwapRB(True) # Swap BGR order to RGB self.net.setPreferableTarget( cv2.dnn.DNN_TARGET_CUDA) # For using CUDA GPU self.net.setPreferableBackend( cv2.dnn.DNN_BACKEND_CUDA) # For using CUDA GPU # Standard Lines self.upper_std_line = 0 # Lighter's standard line which is used for getting height self.lower_std_line = 0 # Lighter's standard line which is used for getting height self.upper_normal_line = 0 # Normal bound line to decide whether lighter has defect or not self.lower_normal_line = 0 # Normal bound line to decide whether lighter has defect or not # QSlider bars # ROI which can be controlled by user self.roi_upper = 0 self.roi_lower = self.display_height self.roi_col = 0 # Lighter's standard line which can be controlled by user self.std_upper = 0 self.std_lower = self.display_height # Lighter Informations self.lighter_width = 0 # Thickness of one lighter self.lighter_height = 0 # Height of lighter # Image filters # Sobel filter is used to extract the outline of an image. self.sobel_filter = cv2.cuda.createSobelFilter(cv2.CV_8U, cv2.CV_8U, 0, 1, ksize=5, scale=0.25) # Hough segment filter is used to extract standard line of lighter. # You can decrease first two arguments if you want higher precision. # 3rd arg: minimum length of line # 4th arg: minimum gap of each lines self.hough_seg_filter = cv2.cuda.createHoughSegmentDetector( 0.1, np.pi / 180, 80, 1) signal.signal( signal.SIGINT, self.sigint_handler) # Allocate Ctrl + C (SIGINT)'s handler. self.initialize_camera() self.load_lighter_info()
def face_detect(sensor_mode=S_MODE_3_1280_720_60, dispW=DISP_W_M3_M4_one_half, dispH=DISP_H_M3_M4_one_half): face_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml") eye_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_eye.xml") left_camera = CSI_Camera() # WS mod: IMPORTANT use lowest fps = 21 as default, otherwise modes 0 or 1 crash: reboot required left_camera.create_gstreamer_pipeline( sensor_id=0, sensor_mode=sensor_mode, framerate=21, flip_method=0, display_height=dispH, display_width=dispW, ) left_camera.open(left_camera.gstreamer_pipeline) left_camera.start() txt = "Face Detect: Sensor Mode {}, Display {} x {}".format( sensor_mode, dispW, dispH) # WS mod cv2.namedWindow(txt, cv2.WINDOW_AUTOSIZE) if (not left_camera.video_capture.isOpened()): # Cameras did not open, or no camera attached print("Unable to open any cameras") # TODO: Proper Cleanup SystemExit(0) try: # Start counting the number of frames read and displayed left_camera.start_counting_fps() while cv2.getWindowProperty(txt, 0) >= 0: img = read_camera(left_camera, False) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) if show_fps: draw_label( img, "Frames Displayed (PS): " + str(left_camera.last_frames_displayed), (10, 20)) draw_label( img, "Frames Read (PS): " + str(left_camera.last_frames_read), (10, 40)) cv2.imshow(txt, img) left_camera.frames_displayed += 1 keyCode = cv2.waitKey(5) & 0xFF # Stop the program on the ESC key if keyCode == 27: break finally: left_camera.stop() left_camera.release() cv2.destroyAllWindows()
def getCapture(): # 반복적으로 화면 캡쳐를 얻는 함수 # 로컬에 화면 캡쳐 이미지를 저장함 camera = CSI_Camera() camera.create_gstreamer_pipeline(sensor_id=0, sensor_mode=0, framerate=30, flip_method=0, display_height=720, display_width=1280) camera.open(camera.gstreamer_pipeline) camera.start() try: # camera.start_counting_fps() while 1: _, img_ori = camera.read() temp = cv2.cvtColor(img_ori, cv2.COLOR_RGB2GRAY) temp = cv2.cuda.cvtColor(img_ori, cv.COLOR_RGB2GRAY) img = np.zeros([720, 720, 3]) for i in range(3): img[:, :, i] = temp[:, 280:1000] img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15) img = cv2.cuda.createSobelFilter(cv.CV_8UC1, -1, 1, 1) img = cv2.convertScaleAbs(img) sub_img = copy.deepcopy(img) print("1") raw = 0 try: print("2") temp, stick = findRaw(img) # 라이터 위치를 특정하기 위한 받침대 위치 확인 print(temp, stick) if temp is not None and temp > 0: # if 0.9*raw < temp < 1.1*raw : print("카메라가 위치를 벗어남") # 이전 프레임과 비교하여 받침대 위치가 벗어나면 카메라가 움직인 것 raw = temp print("3") # 상한~하한선 찾기 print(stick, raw) h_line, m_line, l_line = checkLineRatio(stick, raw) print(h_line, m_line, l_line) cv2.line(sub_img, (0, raw), (720, raw), (255, 255, 0), 1) cv2.line(sub_img, (0, stick), (720, stick), (255, 255, 0), 1) cv2.line(sub_img, (0, h_line), (720, h_line), (0, 255, 0), 1) cv2.line(sub_img, (0, m_line), (720, m_line), (0, 255, 0), 1) cv2.line(sub_img, (0, l_line), (720, l_line), (0, 255, 0), 1) cv2.imwrite("images/" + str(10) + ".jpg", sub_img) except: pass cv2.imshow("Gas Solution", sub_img) time.sleep(10) if (cv2.waitKey(5) & 0xFF) == 27: break finally: camera.stop() camera.release() cv2.destroyAllWindows()