-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp-from-video.py
174 lines (140 loc) · 5.1 KB
/
temp-from-video.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import cv2
import pytesseract
import csv
import time
import os
from PIL import Image
import re
# Function for selecting ROI
roi_selected = False
roi_start_point = None
roi_end_point = None
# Create a directory to save the frames
output_dir = 'frames'
os.makedirs(output_dir, exist_ok=True)
def select_roi(event, x, y, flags, param):
global roi_selected, roi_start_point, roi_end_point
if event == cv2.EVENT_LBUTTONDOWN:
roi_start_point = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
# Ensure top-left and bottom-right are correctly ordered
x1, x2 = sorted([roi_start_point[0], x])
y1, y2 = sorted([roi_start_point[1], y])
roi_start_point, roi_end_point = (x1, y1), (x2, y2)
roi_selected = True
vis_image = cv2.cvtColor(gray.copy(), cv2.COLOR_GRAY2BGR)
cv2.rectangle(vis_image, roi_start_point, roi_end_point, (255, 0, 0), 2)
cv2.imshow("video", vis_image)
# Load the video
# video_path = "HeatCameraText.mp4"
video_path = "35x35V3 T2.MP4"
start_time_seconds = 5
cap = cv2.VideoCapture(video_path)
# Check if video opened successfully
if not cap.isOpened():
print("Error: Could not open video.")
exit()
# Extract the first frame
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame from video.")
cap.release()
exit()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# half resolution
#scale_percent = 90
scale_percent = 50
width = int(gray.shape[1] * scale_percent / 100)
height = int(gray.shape[0] * scale_percent / 100)
dim = (width, height)
gray = cv2.resize(gray, dim, interpolation=cv2.INTER_AREA)
# border_width = 100 # Adjust as needed
# border_color = (0, 0, 0) # Black color
# gray = cv2.copyMakeBorder(gray, border_width, border_width, border_width, border_width, cv2.BORDER_CONSTANT, value=border_color)
# Display the frame and select ROI
cv2.namedWindow("video")
cv2.setMouseCallback("video", select_roi)
cv2.imshow("video", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Initialize CSV file
csv_file = "output.csv"
with open(csv_file, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Time", "Temperature"])
# FPS of the video
fps = int(cap.get(cv2.CAP_PROP_FPS))
#frame_rate = cap.get(cv2.CAP_PROP_FPS)
start_frame = int(start_time_seconds * fps)
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
# Interval for extraction in seconds
interval = 0.2 # change this as required
# Calculate frames to skip
skip_frames = int(fps * interval)
current_time = 0
previous_number = 0
number = 0
while ret:
# Extract number from ROI
#roi = gray
roi = gray[
roi_start_point[1] : roi_end_point[1], roi_start_point[0] : roi_end_point[0]
]
# thr = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
# cv2.THRESH_BINARY, 21, 23)
# resize
#roi = cv2.resize(roi, dim, interpolation=cv2.INTER_AREA)
border_width = 100 # Adjust as needed
border_color = (0, 0, 0) # Black color
roi = cv2.copyMakeBorder(roi, border_width, border_width, border_width, border_width, cv2.BORDER_CONSTANT, value=border_color)
# threshold
#ret, roi = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# smoothing
#roi = cv2.medianBlur(roi, 3)
# show for debugging
cv2.imshow("roi", roi)
frame_filename = os.path.join(output_dir, 'f.jpg')
cv2.imwrite(frame_filename, roi)
cv2.waitKey(1)
text = pytesseract.image_to_string(roi, config="--psm 6")
numbers = re.findall(r'\d+\.\d+|\d+', text)
# numbers = [num for num in text.split() if num.replace(".", "").isinteger()]
# Assuming you're looking for the first number (or change logic accordingly)
# temp = numbers[0] if numbers else "N/A"
if(numbers):
number = float(numbers[1]) if len(numbers) > 1 else float(numbers[0])
print(previous_number)
print(number)
print(type(number))
print(type(previous_number))
# Write time and temperature to CSV
with open(csv_file, "a", newline="") as file:
writer = csv.writer(file)
if abs(number - previous_number) < 50:
<<<<<<< Updated upstream
writer.writerow([current_time, number])
previous_number = number
# if (previous_number):
# previous_number = previous_number[0]
=======
#if number != previous_number:
# writer.writerow([current_time, number])
writer.writerow([str(round(current_time, 2)), number])
previous_number = number
# if (previous_number):
# previous_number = previous_number[0]
else:
writer.writerow([str(round(current_time, 2)), previous_number])
>>>>>>> Stashed changes
# Skip frames and update current_time
cap.set(1, cap.get(1) + skip_frames)
current_time += interval
# Read the next frame
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, dim, interpolation=cv2.INTER_AREA)
# Release video capture object
cap.release()
print(f"Data written to {csv_file}")