-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
77 lines (59 loc) · 2.3 KB
/
tempCodeRunnerFile.py
File metadata and controls
77 lines (59 loc) · 2.3 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
import cv2
import numpy as np
video_path = r'Video/fire3.mp4'
cap = cv2.VideoCapture(video_path)
def flame_mask(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Mask for red/orange/yellow flames (avoid green)
lower_flame = np.array([0, 50, 180]) # H, S, V (V high for brightness, S not too low)
upper_flame = np.array([35, 255, 255]) # Up to yellow, not into green
# Mask for white-hot flame (low saturation, high value)
lower_white = np.array([0, 0, 200])
upper_white = np.array([179, 60, 255]) # S low, V high
mask_flame = cv2.inRange(hsv, lower_flame, upper_flame)
mask_white = cv2.inRange(hsv, lower_white, upper_white)
# Combine both masks
mask = cv2.bitwise_or(mask_flame, mask_white)
return mask
ret, prev_frame = cap.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 1. Mask for flame regions
mask = flame_mask(frame)
cv2.imshow('Flame Mask', mask) # Visualize the flame mask
# 2. Optical flow
flow = cv2.calcOpticalFlowFarneback(
prev_gray, gray, None,
pyr_scale=0.5, levels=3, winsize=15, iterations=3,
poly_n=5, poly_sigma=1.2, flags=0
)
# 3. Average flow over flame pixels
mask_bool = mask.astype(bool)
fx = flow[..., 0][mask_bool]
fy = flow[..., 1][mask_bool]
if fx.size > 0 and fy.size > 0:
avg_fx = np.mean(fx)
avg_fy = np.mean(fy)
else:
avg_fx = 0
avg_fy = 0
# 4. Draw the average direction
h, w = gray.shape
center_x, center_y = w // 2, h // 2
scale = 50
end_point = (int(center_x + avg_fx * scale), int(center_y + avg_fy * scale))
cv2.circle(frame, (center_x, center_y), 8, (0, 255, 0), -1) # Start: green
cv2.circle(frame, end_point, 8, (0, 0, 255), -1) # End: red
cv2.arrowedLine(frame, (center_x, center_y), end_point, (0, 255, 255), 4, tipLength=0.2)
cv2.putText(frame, f"Flame Flow: ({avg_fx:.2f}, {avg_fy:.2f})", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Flame Optical Flow', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
prev_gray = gray.copy()
cap.release()
cv2.destroyAllWindows()