live camera preview

This commit is contained in:
Lincoln-LM 2022-01-07 10:05:59 -07:00
parent 7947d1b5ca
commit 8af665dd60

View File

@ -430,6 +430,7 @@ class Application(tk.Frame):
def previewing_work(self): def previewing_work(self):
last_frame_tk = None last_frame_tk = None
last_camera = self.config_json["camera"]
if self.config_json["MonitorWindow"]: if self.config_json["MonitorWindow"]:
from windowcapture import WindowCapture from windowcapture import WindowCapture
@ -451,12 +452,25 @@ class Application(tk.Frame):
while self.previewing: while self.previewing:
if self.config_json["camera"] != last_camera:
video = cv2.VideoCapture(self.config_json["camera"],backend)
video.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
video.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
video.set(cv2.CAP_PROP_BUFFERSIZE,1)
print(f"camera {self.config_json['camera']}")
last_camera = self.config_json["camera"]
eye = self.player_eye eye = self.player_eye
w, h = eye.shape[::-1] w, h = eye.shape[::-1]
roi_x, roi_y, roi_w, roi_h = self.config_json["view"] roi_x, roi_y, roi_w, roi_h = self.config_json["view"]
_, frame = video.read() _, frame = video.read()
if frame is not None:
if not self.config_json["MonitorWindow"]: if not self.config_json["MonitorWindow"]:
frame = cv2.resize(frame,(960,540)) size = frame.shape[::-1]
_, fw, fh = size
if fw >= 1920:
frame = cv2.resize(frame,(960,round(fh/fw*960)))
roi = cv2.cvtColor(frame[roi_y:roi_y+roi_h,roi_x:roi_x+roi_w],cv2.COLOR_RGB2GRAY) roi = cv2.cvtColor(frame[roi_y:roi_y+roi_h,roi_x:roi_x+roi_w],cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(roi,eye,cv2.TM_CCOEFF_NORMED) res = cv2.matchTemplate(roi,eye,cv2.TM_CCOEFF_NORMED)
_, match, _, max_loc = cv2.minMaxLoc(res) _, match, _, max_loc = cv2.minMaxLoc(res)