-
Notifications
You must be signed in to change notification settings - Fork 5
/
paper_streaming.py
121 lines (98 loc) · 3.68 KB
/
paper_streaming.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import argparse
import os
import sys
import time
import urllib
from distutils import util
from threading import Lock
import cv2
import numpy as np
from PyQt5 import QtWidgets, uic
from PyQt5 import QtCore, QtGui
import _thread
from libs.config import *
from libs.hand_remover.hand_remover import HandRemover
from libs.paper_processor.paper_processor import PaperProcessor
from libs.stroke_filter.stroke_filter import StrokeFilter
from libs.utils.common import *
from libs.utils.ui_utils import *
from libs.webcam import pyfakewebcam
OUTPUT_SIMULATED_CAMERA = True
paper_processor = PaperProcessor(REFERENCE_ARUCO_IMAGE_PATH, aruco_remove_mask_path=REFERENCE_ARUCO_REMOVE_IMAGE_PATH, smooth=True, debug=False, output_video_path=None)
hand_remover = HandRemover()
stroke_filter = StrokeFilter()
# create output video stream
if OUTPUT_SIMULATED_CAMERA:
output_width, output_height = paper_processor.get_output_size()
camera = pyfakewebcam.FakeWebcam(get_camera_path("PaperStreamCam"), output_width, output_height)
frame = None
frame_mutex = Lock()
# camera reading thread
new_camera_url = None
def camera_reading_thread():
global frame, frame_mutex, new_camera_url
cap = cv2.VideoCapture(DEFAULT_WEBCAM_URL)
status_label = ""
while(True):
if new_camera_url is not None:
print("Opening new camera at: {}".format(new_camera_url))
try:
cap.release()
except:
pass
cap = cv2.VideoCapture(new_camera_url)
new_camera_url = None
frame_mutex.acquire()
ret, frame = cap.read()
frame_mutex.release()
new_status_label = "Camera status: Disconnected"
if ret:
new_status_label = "Camera status: Connected"
if new_status_label != status_label:
status_label = new_status_label
window.cameraStatusLabel.setText(status_label)
time.sleep(0.05)
# processing thread
def processing_thread():
global frame, frame_mutex
while(True):
image = None
frame_mutex.acquire()
try:
image = frame.copy()
except:
pass
frame_mutex.release()
if image is None:
continue
# get paper image
start_time = time.time()
is_cropped, processed_image = paper_processor.get_paper_image(image)
# print("Paper transform. time: {}".format(time.time() - start_time))
# remove hand
start_time = time.time()
processed_image = hand_remover.process(processed_image, is_cropped=is_cropped)
# print("Hand removal time: {}".format(time.time() - start_time))
# post processing
start_time = time.time()
processed_image = stroke_filter.process(processed_image)
# print("Post processing time: {}".format(time.time() - start_time))
if OUTPUT_SIMULATED_CAMERA:
if is_cropped:
camera_frame = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
camera.schedule_frame(camera_frame)
def connect_clicked():
global new_camera_url
new_camera_url = window.cameraUrlInput.toPlainText()
# setup app
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi("libs/main_window.ui")
window.setWindowTitle("PaperStream")
window.newRoomBtn.clicked.connect(new_room_clicked)
window.joinRoomBtn.clicked.connect(join_room_clicked)
window.connectBtn.clicked.connect(connect_clicked)
window.cameraUrlInput.setPlainText(DEFAULT_WEBCAM_URL)
window.show()
_thread.start_new_thread( camera_reading_thread, () )
_thread.start_new_thread( processing_thread, () )
app.exec()