-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_camera_test.py
More file actions
121 lines (94 loc) · 3.98 KB
/
simple_camera_test.py
File metadata and controls
121 lines (94 loc) · 3.98 KB
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
#!/usr/bin/env python3
"""
Simplified GUI test for camera feed display
"""
import tkinter as tk
from tkinter import ttk
import cv2
from PIL import Image, ImageTk
import threading
import time
class SimpleCameraGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Camera Feed Test")
self.root.geometry("800x600")
self.cap = None
self.is_running = False
self.current_frame = None
self.setup_gui()
def setup_gui(self):
# Control frame
control_frame = ttk.Frame(self.root)
control_frame.pack(pady=10)
ttk.Button(control_frame, text="Start Camera", command=self.start_camera).pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame, text="Stop Camera", command=self.stop_camera).pack(side=tk.LEFT, padx=5)
# Video display
self.video_canvas = tk.Canvas(self.root, bg='black', width=640, height=480)
self.video_canvas.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)
# Status label
self.status_label = ttk.Label(self.root, text="Camera not started")
self.status_label.pack(pady=5)
# Start GUI update loop
self.update_gui()
def start_camera(self):
try:
self.cap = cv2.VideoCapture(0)
if not self.cap.isOpened():
self.status_label.config(text="❌ Failed to open camera")
return
self.is_running = True
self.status_label.config(text="✅ Camera started")
# Start capture thread
self.capture_thread = threading.Thread(target=self.capture_loop)
self.capture_thread.daemon = True
self.capture_thread.start()
except Exception as e:
self.status_label.config(text=f"❌ Error: {str(e)}")
def stop_camera(self):
self.is_running = False
if self.cap:
self.cap.release()
self.cap = None
self.status_label.config(text="Camera stopped")
self.video_canvas.delete("all")
def capture_loop(self):
while self.is_running and self.cap:
ret, frame = self.cap.read()
if ret and frame is not None:
self.current_frame = frame.copy()
time.sleep(0.03) # ~30 FPS
def update_gui(self):
if self.current_frame is not None:
self.display_frame()
self.root.after(16, self.update_gui) # ~60 FPS
def display_frame(self):
try:
# Convert BGR to RGB
frame_rgb = cv2.cvtColor(self.current_frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(frame_rgb)
# Get canvas dimensions
canvas_width = self.video_canvas.winfo_width()
canvas_height = self.video_canvas.winfo_height()
if canvas_width <= 1 or canvas_height <= 1:
canvas_width = 640
canvas_height = 480
# Resize image to fit canvas
img_width, img_height = pil_image.size
ratio = min(canvas_width / img_width, canvas_height / img_height)
new_width = int(img_width * ratio)
new_height = int(img_height * ratio)
pil_image = pil_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# Convert to PhotoImage and display
photo = ImageTk.PhotoImage(pil_image)
# Clear canvas and add image
self.video_canvas.delete("all")
self.video_canvas.create_image(canvas_width//2, canvas_height//2, anchor=tk.CENTER, image=photo)
self.video_canvas.image = photo # Keep reference
except Exception as e:
print(f"Error displaying frame: {e}")
def run(self):
self.root.mainloop()
if __name__ == "__main__":
app = SimpleCameraGUI()
app.run()