Advertisement
MihirP007

Final Py code

Dec 8th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | Source Code | 0 0
  1. import cv2
  2. import csv
  3. 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
  4. from datetime import datetime, timedelta
  5.  
  6. db_file = "db.csv"
  7. log_file = "log.csv"
  8.  
  9. with open(db_file, "w", newline='') as db:
  10.     db_writer = csv.writer(db)
  11.     db_writer.writerow(["Classroom", "Student", "Entry Time", "Status"])
  12.  
  13. active_people = {}
  14. last_student = 0
  15. last_student_exit = 0
  16. classroom = 1201  
  17. timetable = {8:"OSY",9:"DBMS",10:"STE",12:"EVN",15:"AJP",23:"CSS"}
  18.  
  19. cap = cv2.VideoCapture(0)
  20. cap.set(3, 640)
  21. cap.set(4, 480)
  22.  
  23. def diff(start, end, secs):
  24.     end_time = datetime.strptime(end, '%H:%M:%S')
  25.     start_time = datetime.strptime(start, '%H:%M:%S')
  26.     diff = min(end_time - start_time, start_time - end_time)
  27.     return abs(diff) >= timedelta(seconds=secs)
  28. def decode(image, symbols=None):
  29.     pixels, width, height = _pixel_data(image)
  30.  
  31.     results = []
  32.     with _image_scanner() as scanner:
  33.         if symbols:
  34.             disable = set(ZBarSymbol).difference(symbols)
  35.             for symbol in disable:
  36.                 zbar_image_scanner_set_config(scanner, symbol, ZBarConfig.CFG_ENABLE, 0)
  37.             for symbol in symbols:
  38.                 zbar_image_scanner_set_config(scanner, symbol, ZBarConfig.CFG_ENABLE, 1)
  39.         with _image() as img:
  40.             zbar_image_set_format(img, _FOURCC['L800'])
  41.             zbar_image_set_size(img, width, height)
  42.             zbar_image_set_data(img, cast(pixels, c_void_p), len(pixels), None)
  43.             decoded = zbar_scan_image(scanner, img)
  44.             if decoded < 0:
  45.                 raise PyZbarError('Unsupported image format')
  46.             else:
  47.                 results.extend(_decode_symbols(_symbols_for_image(img)))
  48.  
  49.     return results
  50. while True:
  51.     success, img = cap.read()
  52.  
  53.     for barcode in decode(img):
  54.         student = barcode.data.decode('utf-8')
  55.         now = datetime.now().strftime('%H:%M:%S')
  56.  
  57.         if student in active_people.keys():
  58.             student_exit_time = datetime.now().strftime('%H:%M:%S')
  59.             if diff(active_people[student], student_exit_time, 10):
  60.                 print(student, "joined at", active_people[student], "and left at", student_exit_time)
  61.                 last_student = student
  62.                 last_student_exit = student_exit_time
  63.  
  64.  
  65.                 with open(db_file, "r", newline='') as db_read:
  66.                     rows = list(csv.reader(db_read))
  67.                     rows = [row for row in rows if row[1] != student]
  68.  
  69.                 with open(db_file, "w", newline='') as db_write:
  70.                     db_writer = csv.writer(db_write)
  71.                     db_writer.writerows(rows)
  72.  
  73.                 with open(log_file, "a", newline='') as log:
  74.                     log_writer = csv.writer(log)
  75.                     log_writer.writerow([classroom,student,last_student_exit,now])
  76.  
  77.                 active_people.pop(student)
  78.         else:
  79.             # if student == last_student and diff(last_student_exit, student_exit_time, 60):
  80.             if student != last_student:
  81.                 if int(now.split(":")[0]) in timetable.keys():
  82.                     data = [classroom,student,now,"Yes"]
  83.                 else:
  84.                     data = [classroom,student,now,"No"]
  85.                 with open(db_file, "a", newline='') as db:
  86.                     db_writer = csv.writer(db)
  87.                     db_writer.writerow(data)
  88.                 last_student = student
  89.                 active_people[student] = now
  90.  
  91.         print("Current Students:", active_people, ", Total Students:", len(active_people))
  92.  
  93.     cv2.imshow('Result', img)
  94.     cv2.waitKey(50)
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement