-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
106 lines (81 loc) · 3.8 KB
/
scanner.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
import cv2
import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from pyzbar.pyzbar import decode
from playsound import playsound # Make sure to install playsound using `pip install playsound`
def scan_barcode(save_to_csv=False):
"""Scans barcodes from the webcam, interacts with a web form, and optionally saves data to CSV.
Args:
save_to_csv (bool, optional): If True, scanned barcode data will be saved to a CSV file. Defaults to False.
"""
try:
# Initialize Selenium driver
driver = webdriver.Chrome() # Pastikan chromedriver sesuai dengan versi Chrome
driver.get("http://127.0.0.1:8000/sales/creates")
# Initialize webcam capture
cap = cv2.VideoCapture(0) # Try changing the index if needed
if not cap.isOpened():
print("Gagal membuka kamera. Pastikan DroidCam aktif dan terhubung dengan benar.")
return
last_save_time = None
csv_file = None
csv_writer = None
if save_to_csv:
csv_file = open('scanned_barcodes.csv', 'w', newline='')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Timestamp', 'Barcode'])
while True:
ret, frame = cap.read()
if not ret:
print("Gagal membaca frame dari kamera.")
break
# Display the frame in a window
cv2.imshow('Webcam Feed', frame)
# Decode barcodes from the frame
barcodes = decode(frame)
if barcodes:
for barcode in barcodes:
barcode_data = barcode.data.decode("utf-8")
print(f"Barcode ditemukan: {barcode_data}")
# Check timestamp for saving to CSV
current_time = time.time()
if last_save_time is None or (current_time - last_save_time > 3):
# Update last save time
last_save_time = current_time
try:
# Interact with the web form
input_element = driver.find_element(By.ID, "barcode_input")
input_element.clear()
input_element.send_keys(barcode_data)
if save_to_csv:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
csv_writer.writerow([timestamp, barcode_data])
print(f"Barcode {barcode_data} berhasil dipindai.")
# Play beep sound
playsound('Beep.mp3') # Adjust the file path as needed
# Submit the form by pressing Enter
input_element.send_keys(Keys.ENTER)
print("Form telah disubmit dengan menekan Enter.")
except Exception as selenium_error:
print(f"Terjadi kesalahan saat berinteraksi dengan form: {selenium_error}")
else:
print(f"Barcode {barcode_data} sudah dipindai dalam 3 detik terakhir, tidak disimpan.")
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Add a short delay to reduce CPU usage
# time.sleep(0.1)
except Exception as e:
print(f"Terjadi kesalahan: {e}")
finally:
# Clean up resources
if csv_file:
csv_file.close()
cap.release()
cv2.destroyAllWindows()
driver.quit()
if __name__ == "__main__":
scan_barcode(save_to_csv=False) # Change to True if saving to CSV