-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode_detect.py
49 lines (37 loc) · 1.53 KB
/
barcode_detect.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
import cv2
from pyzbar import pyzbar
def detect_barcodes_in_video(video_path, output_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error: Could not open video.")
return
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
codec = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4 file
out = cv2.VideoWriter(output_path, codec, fps, (frame_width, frame_height))
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
barcodes = pyzbar.decode(gray)
for barcode in barcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
barcode_data = barcode.data.decode("utf-8")
barcode_type = barcode.type
text = f"{barcode_data} ({barcode_type})"
cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
print(f"Found barcode: {barcode_data} ({barcode_type})")
out.write(frame)
cv2.imshow("Barcode Detector", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
video_path = "/home/anirudh/Videos/tiburon_simulation.mp4"
output_path = "output_video_with_barcodes.mp4"
detect_barcodes_in_video(video_path, output_path)