Advertisement
orneto

catraca.py

Jun 23rd, 2023 (edited)
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import time
  4. import serial
  5. from pyzbar.pyzbar import decode
  6. from datetime import datetime
  7.  
  8. carro = {
  9.     "num" : "",
  10.     "entrada": "",
  11.     "saida": "",
  12.     "cartao":""
  13. }
  14.  
  15. estadosCartao = {
  16.     "Done",
  17.     "Invalid"
  18. }
  19.  
  20. class carro:
  21.     def __init__(self, tag_in):
  22.         self.tag = tag_in
  23.         self.entrada = 0
  24.         self.saida = 0
  25.         self.cartao = ""
  26.     def register(self,timein):
  27.         if self.entrada != 0 and self.saida == 0:
  28.             self.saida = timein
  29.             self.calcularCusto()
  30.             time.sleep(2)
  31.             if self.consultarcartao():
  32.                 ser.write(b"S")
  33.             else:
  34.                 ser.write(b'\b')
  35.                 ser.write(b"Cartao Invalido\n")
  36.         else:
  37.             self.entrada = timein
  38.             ser.write(b"E")
  39.             self.saida = 0
  40.             self.cartao = ""
  41.     def calcularCusto(self):
  42.         tempo = int(self.saida - self.entrada)
  43.         print("Tempo decorrido:" , tempo , "Valor :", tempo*0.25)
  44.         ser.write(b'\b')
  45.         ser.write(str("T:"+  str(tempo) + " R$:" + str(tempo/4)).encode())
  46.         ser.write(b"\n")
  47.     def consultarcartao(self):
  48.         if self.tag == "car1":
  49.             self.cartao = "Invalid"
  50.             return 0
  51.         else:
  52.             self.cartao = "Done"
  53.             return 1
  54.     def print(self):
  55.         print(self.tag,":", self.entrada,"-",self.saida,"   ",self.cartao)
  56.  
  57. def getcar(carvect, cartag):
  58.     for i in range(len(carvect)):
  59.         #print(carvect[i].tag,cartag,"comp:",carvect[i].tag== cartag)
  60.         if carvect[i].tag == cartag:
  61.             return i
  62.     newcar = carro(cartag)
  63.     carvect.append(newcar)
  64.     return (len(carvect)-1)
  65. # img = cv2.imread('1.png')
  66. cap = cv2.VideoCapture(0)
  67. cap.set(3, 640)
  68. cap.set(4, 480)
  69.  
  70. carros = []
  71.  
  72. notcon = True
  73. ser = 0
  74. while notcon:
  75.     try:
  76.         ser = serial.Serial("COM5", 115200)
  77.         notcon = False
  78.     except:
  79.         print("Not connected")
  80.         time.sleep(1)
  81.  
  82.  
  83. while True:
  84.     success, img = cap.read()
  85.     rst = ""
  86.     for barcode in decode(img):
  87.         myData = barcode.data.decode('utf-8')
  88.         pts = np.array([barcode.polygon], np.int32)
  89.         pts = pts.reshape((-1, 1, 2))
  90.         cv2.polylines(img, [pts], True, (255, 0, 255), 5)
  91.         pts2 = barcode.rect
  92.         cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 255), 2)
  93.         rst = myData
  94.     if rst != "":
  95.         c = getcar(carros,rst)
  96.         carros[c]
  97.         carros[c].register(time.time())
  98.         carros[c].print()
  99.         rst = ""
  100.         time.sleep(1)
  101.     cv2.imshow('Result', img)
  102.     cv2.waitKey(1)
  103.     #print("mydata é: ",decode(img))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement