forked from HamzahJomaa/Google-Gemini-Live-Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_stream.py
39 lines (33 loc) · 1.17 KB
/
video_stream.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
import cv2
import threading
from PIL import Image, ImageTk
import tkinter as tk
class VideoStreamHandler:
def __init__(self, root, canvas):
self.root = root
self.canvas = canvas
self.cap = None
self.photo = None
self.current_frame = None
def start_stream(self, camera_index):
if self.cap is not None:
self.cap.release()
self.cap = cv2.VideoCapture(int(camera_index))
thread = threading.Thread(target=self.video_stream)
thread.start()
def video_stream(self):
while self.cap.isOpened():
ret, frame = self.cap.read()
if ret:
self.current_frame = frame
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(cv2image)
self.photo = ImageTk.PhotoImage(image=img)
self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
self.root.update()
def stop_video(self):
if self.cap is not None and self.cap.isOpened():
self.cap.release()
self.root.destroy()
def get_current_frame(self):
return self.current_frame