46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import cv2
|
|
import requests
|
|
# source path/to/venv/bin/activate
|
|
API_URL = "http://localhost:8000/checkin" # Đổi lại nếu backend chạy ở địa chỉ khác
|
|
CAMERA_ID = "cam_pc_01"
|
|
|
|
def capture_and_checkin():
|
|
cap = cv2.VideoCapture(0) # Dùng camera mặc định (webcam)
|
|
|
|
if not cap.isOpened():
|
|
print("Không mở được camera.")
|
|
return
|
|
|
|
print("Đang mở camera. Nhấn phím 'c' để check-in, 'q' để thoát.")
|
|
while True:
|
|
ret, frame = cap.read()
|
|
if not ret:
|
|
print("Không đọc được frame.")
|
|
break
|
|
|
|
cv2.imshow("Camera", frame)
|
|
|
|
key = cv2.waitKey(1)
|
|
if key == ord("q"):
|
|
break
|
|
elif key == ord("c"):
|
|
# Ghi tạm ảnh ra file
|
|
filename = "frame.jpg"
|
|
cv2.imwrite(filename, frame)
|
|
|
|
# Gửi ảnh lên server
|
|
with open(filename, "rb") as f:
|
|
response = requests.post(
|
|
API_URL,
|
|
files={"file": ("frame.jpg", f, "image/jpeg")},
|
|
data={"camera_id": CAMERA_ID}
|
|
)
|
|
|
|
print("📡 Server:", response.json())
|
|
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|
|
|
|
if __name__ == "__main__":
|
|
capture_and_checkin()
|