Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import csv
- from pyzbar.pyzbar import _pixel_data,_image_scanner,ZBarSymbol,zbar_image_scanner_set_config,ZBarConfig,zbar_image_set_format,_image,zbar_image_set_size,zbar_image_set_data,cast,zbar_scan_image,_decode_symbols,_symbols_for_image,PyZbarError,c_void_p,_FOURCC
- from datetime import datetime, timedelta
- db_file = "db.csv"
- log_file = "log.csv"
- with open(db_file, "w", newline='') as db:
- db_writer = csv.writer(db)
- db_writer.writerow(["Classroom", "Student", "Entry Time", "Status"])
- active_people = {}
- last_student = 0
- last_student_exit = 0
- classroom = 1201
- timetable = {8:"OSY",9:"DBMS",10:"STE",12:"EVN",15:"AJP",23:"CSS"}
- cap = cv2.VideoCapture(0)
- cap.set(3, 640)
- cap.set(4, 480)
- def diff(start, end, secs):
- end_time = datetime.strptime(end, '%H:%M:%S')
- start_time = datetime.strptime(start, '%H:%M:%S')
- diff = min(end_time - start_time, start_time - end_time)
- return abs(diff) >= timedelta(seconds=secs)
- def decode(image, symbols=None):
- pixels, width, height = _pixel_data(image)
- results = []
- with _image_scanner() as scanner:
- if symbols:
- disable = set(ZBarSymbol).difference(symbols)
- for symbol in disable:
- zbar_image_scanner_set_config(scanner, symbol, ZBarConfig.CFG_ENABLE, 0)
- for symbol in symbols:
- zbar_image_scanner_set_config(scanner, symbol, ZBarConfig.CFG_ENABLE, 1)
- with _image() as img:
- zbar_image_set_format(img, _FOURCC['L800'])
- zbar_image_set_size(img, width, height)
- zbar_image_set_data(img, cast(pixels, c_void_p), len(pixels), None)
- decoded = zbar_scan_image(scanner, img)
- if decoded < 0:
- raise PyZbarError('Unsupported image format')
- else:
- results.extend(_decode_symbols(_symbols_for_image(img)))
- return results
- while True:
- success, img = cap.read()
- for barcode in decode(img):
- student = barcode.data.decode('utf-8')
- now = datetime.now().strftime('%H:%M:%S')
- if student in active_people.keys():
- student_exit_time = datetime.now().strftime('%H:%M:%S')
- if diff(active_people[student], student_exit_time, 10):
- print(student, "joined at", active_people[student], "and left at", student_exit_time)
- last_student = student
- last_student_exit = student_exit_time
- with open(db_file, "r", newline='') as db_read:
- rows = list(csv.reader(db_read))
- rows = [row for row in rows if row[1] != student]
- with open(db_file, "w", newline='') as db_write:
- db_writer = csv.writer(db_write)
- db_writer.writerows(rows)
- with open(log_file, "a", newline='') as log:
- log_writer = csv.writer(log)
- log_writer.writerow([classroom,student,last_student_exit,now])
- active_people.pop(student)
- else:
- # if student == last_student and diff(last_student_exit, student_exit_time, 60):
- if student != last_student:
- if int(now.split(":")[0]) in timetable.keys():
- data = [classroom,student,now,"Yes"]
- else:
- data = [classroom,student,now,"No"]
- with open(db_file, "a", newline='') as db:
- db_writer = csv.writer(db)
- db_writer.writerow(data)
- last_student = student
- active_people[student] = now
- print("Current Students:", active_people, ", Total Students:", len(active_people))
- cv2.imshow('Result', img)
- cv2.waitKey(50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement