123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import cv2
- import pygame
- from pyzbar.pyzbar import decode
- # Initialize pygame for audio playback
- pygame.mixer.init()
- print("Pygame initialized.")
- # Function to play audio
- def play_audio(file_path):
- print(f"Attempting to play audio: {file_path}")
- pygame.mixer.music.load(file_path)
- pygame.mixer.music.play()
- while pygame.mixer.music.get_busy():
- continue
- print("Finished playing audio.")
- # Capture video from camera
- cap = cv2.VideoCapture(0)
- if not cap.isOpened():
- print("Error: Could not access the camera.")
- exit()
- print("Camera accessed successfully.")
- while True:
- ret, frame = cap.read()
- if not ret:
- print("Failed to grab frame.")
- break
- # Decode the QR code in the frame
- decoded_objects = decode(frame)
- if decoded_objects:
- print(f"Decoded {len(decoded_objects)} QR code(s).")
-
- for obj in decoded_objects:
- qr_data = obj.data.decode('utf-8')
- print(f"QR Code detected, data: {qr_data}")
- # Check if the QR code content is "hydrosense"
- if qr_data == "hydrosense":
- audio_file_path = "/home/pi/audio/hydrosense.mp3" # Modify this path if needed
- play_audio(audio_file_path)
-
- # Display the frame
- cv2.imshow("QR Code Scanner", frame)
- # Break the loop if 'q' is pressed
- if cv2.waitKey(1) & 0xFF == ord('q'):
- print("Exiting.")
- break
- # Release the capture and close windows
- cap.release()
- cv2.destroyAllWindows()
- print("Camera released and windows closed.")
|