-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
102 lines (83 loc) · 3.38 KB
/
Copy pathcamera.py
File metadata and controls
102 lines (83 loc) · 3.38 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
import cv2
import time
import os
cap = cv2.VideoCapture(0)
# Настройки камеры (опционально)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FPS, 30)
# Состояния
recording = False
video_writer = None
filter_mode = 0 # 0=обычный, 1=серый, 2=размытие, 3=Canny края
photo_count = 0
os.makedirs("photos", exist_ok=True)
print("Управление:")
print(" S — сохранить фото")
print(" R — начать/остановить запись видео")
print(" F — сменить фильтр (0→1→2→3)")
print(" Q — выход")
while True:
ret, frame = cap.read()
if not ret:
print("Ошибка камеры")
break
# === Применяем фильтр ===
display = frame.copy()
if filter_mode == 1:
# Чёрно-белый
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
display = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) # обратно в BGR для imshow
elif filter_mode == 2:
# Размытие (Gaussian Blur)
display = cv2.GaussianBlur(frame, (21, 21), 0)
elif filter_mode == 3:
# Обнаружение краёв (Canny)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
display = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
# === HUD — текст на экране ===
filter_names = ["Normal", "Grayscale", "Blur", "Canny Edges"]
cv2.putText(display, f"Filter: {filter_names[filter_mode]}",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.putText(display, f"Photos: {photo_count}",
(10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
if recording:
# Мигающий красный REC
if int(time.time() * 2) % 2 == 0:
cv2.circle(display, (display.shape[1] - 30, 30), 12, (0, 0, 255), -1)
cv2.putText(display, "REC", (display.shape[1] - 80, 38),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
video_writer.write(frame) # записываем оригинал без фильтра
cv2.imshow("Camera", display)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
# Сохраняем с временной меткой
filename = f"photos/photo_{int(time.time())}.png"
cv2.imwrite(filename, frame)
photo_count += 1
print(f"Фото сохранено: {filename}")
elif key == ord('r'):
if not recording:
# Начинаем запись
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
filename = f"video_{int(time.time())}.mp4"
h, w = frame.shape[:2]
video_writer = cv2.VideoWriter(filename, fourcc, 20.0, (w, h))
recording = True
print(f"Запись началась: {filename}")
else:
# Останавливаем
recording = False
video_writer.release()
video_writer = None
print("Запись остановлена!")
elif key == ord('f'):
filter_mode = (filter_mode + 1) % 4 # цикл 0→1→2→3→0
# Освобождаем ресурсы
if recording and video_writer:
video_writer.release()
cap.release()
cv2.destroyAllWindows()