Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.65 KB | Source Code | 0 0
  1. from customtkinter import CTk
  2. from customtkinter import CTkCanvas as Canvas, CTkEntry as Entry, CTkTextbox as Textbox, CTkButton as Button, \
  3.     CTkLabel as Label, CTkTabview as Tabview, CTkFrame as Frame, CTkScrollableFrame as ScrollableFrame, \
  4.     CTkCheckBox as CheckBox, CTkImage
  5. from app.views.classes import CTkMyTabview as MyTabView, CTkMyScrollableFrame as myscrollableframe
  6. from app.views.classes import data, InternetManager
  7.  
  8. import cv2
  9. from pyzbar.pyzbar import decode, ZBarSymbol
  10. from PIL import Image, ImageTk
  11.  
  12. from logging import debug, info, warning, error, critical
  13.  
  14. import re
  15.  
  16. from multiprocessing import Process, Queue
  17.  
  18.  
  19. camera_id = 0
  20. delay = 1
  21. testing = data.testing
  22.  
  23.  
  24. class DataInputScreen:
  25.     def __init__(self, tab: Frame, root: CTk, tv: MyTabView):
  26.         self.tv = tv
  27.         self.tab = tab
  28.         self.root = root
  29.         self.cap: cv2.VideoCapture = None
  30.         self.menu = Frame(self.tab)
  31.         self.vid_frame = Frame(self.tab)
  32.  
  33.         self.vid = Label(self.vid_frame, text="")
  34.         self.vid.pack(side='right', expand=True, padx=5, pady=5)
  35.  
  36.         self.menu.place(x=0, y=0, relwidth=.4, relheight=1)
  37.         self.vid_frame.place(relx=0.4, y=0, relwidth=.6, relheight=1)
  38.  
  39.         # if not testing:
  40.         #     self.cap = cv2.VideoCapture(camera_id)
  41.         #     self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1000)
  42.         #     self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1000)
  43.  
  44.         # Menu Setup
  45.         self.scrollabaleframe = myscrollableframe(self.menu, fg_color=data.lightBackgroudColor, corner_radius=30)
  46.         self.scrollabaleframe.pack(expand=True, fill="both", padx=10, pady=10)
  47.         self.send_button = Button(self.menu, text="Send", height=60, corner_radius=400, command=self._send)
  48.         self.send_button.configure(state="disabled")
  49.         self.send_button.cget("font").configure(size=46)
  50.         self.send_button.pack(fill="x", padx=10, pady=(0, 10))
  51.  
  52.         self._previous_tab = ""
  53.  
  54.         self._temp_data: str = ""
  55.  
  56.         self._data_widgets = []
  57.         self._reviewing = False
  58.         self._looking_for_1_half = False
  59.         self._looking_for_2_half = False
  60.         self._looking = False
  61.         self._update_list = False
  62.  
  63.         self._vcmd = (self.root.register(self._validate), "%P")
  64.  
  65.         if testing:
  66.             test_img = CTkImage(light_image=Image.open("app/res/test/test1080p.png"), size=(1920, 1080))
  67.             self.vid.photo_image = test_img
  68.             self.vid.configure(image=test_img)
  69.             Button(self.vid, text="Extend _Data", command=self._refresh, corner_radius=1000, height=50).place(relx=.35, rely=.475, relwidth=.3)
  70.             self._update_list = True
  71.  
  72.         self.q = Queue()
  73.  
  74.         # Create a Process that will execute the capture_frames function
  75.         Process(target=self.empty, args=(self.q,)).start()
  76.  
  77.     def empty(self):
  78.         pass
  79.  
  80.     def _refresh(self):
  81.         InternetManager.data.extend(self._team_data_packet_to_list(data.temp_team_data))
  82.         self._update_list = True
  83.  
  84.     def loop(self):
  85.         # if not testing:
  86.         #     self._open_camera()
  87.         if self.q.empty():
  88.             opencv_image = self.q.get()
  89.             captured_image = Image.fromarray(opencv_image)
  90.             photo_image = ImageTk.PhotoImage(image=captured_image)
  91.             self.vid.photo_image = photo_image
  92.             self.vid.configure(image=photo_image)
  93.  
  94.         self._menu_loop()
  95.  
  96.     def _cam_loop(self, queue):
  97.         # if not self.tv.get() == self._previous_tab:
  98.         self.cap = cv2.VideoCapture(camera_id)
  99.         self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1000)
  100.         self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1000)
  101.  
  102.         # if self.tv.get() == "Data Input":
  103.         while True:
  104.             print("sdakjdbjlosa")
  105.             # self._open_camera(queue)
  106.         # else:
  107.         #     self.cap.release()
  108.         # self._previous_tab = self.tv.get()
  109.  
  110.     def _menu_loop(self):
  111.         if self._update_list:
  112.             for i in InternetManager.data:
  113.                 boxframe = Frame(self.scrollabaleframe, fg_color=data.backgroundColor,
  114.                                  bg_color=data.lightBackgroudColor, corner_radius=30)
  115.                 boxframe.pack(expand=True, fill="x", padx=1, pady=5)
  116.                 self._data_widgets.append(boxframe)
  117.                 for x, y in i.items():
  118.                     if type(y) == int:
  119.                         f = Frame(boxframe)
  120.                         f.pack()
  121.                         Label(f, text=x + ": ").pack(side="left", padx=(15, 0), pady=7)
  122.                         t = Entry(f, placeholder_text="Int", validate="key", validatecommand=self._vcmd, width=100)
  123.                         t.insert(0, str(y))
  124.                         t.configure(state="disabled")
  125.                         t.pack(side="left")
  126.                     elif type(y) == bool:
  127.                         if y:
  128.                             t = CheckBox(boxframe, text=x, onvalue="True", offvalue="False")
  129.                             t.select()
  130.                             t.configure(state="disabled")
  131.                             t.pack()
  132.                         else:
  133.                             t = CheckBox(boxframe, text=x, onvalue="True", offvalue="False")
  134.                             t.deselect()
  135.                             t.configure(state="disabled")
  136.                             t.pack()
  137.                     elif type(y) == str:
  138.                         f = Frame(boxframe)
  139.                         f.pack()
  140.                         Label(f, text=x + ": ").pack(side="left", padx=(15, 0), pady=7)
  141.                         t = Entry(f, placeholder_text="Text", width=200)
  142.                         t.insert(0, y)
  143.                         t.configure(state="disabled")
  144.                         t.pack(side="left")
  145.             self._update_list = False
  146.  
  147.     def _validate(self, val):
  148.         if re.fullmatch("^[-]?\d+$", val) is None:
  149.             return False
  150.         return True
  151.  
  152.     def _send(self):
  153.         InternetManager.upload()
  154.         if not data.testing:
  155.             self._update_list = True
  156.             self.send_button.configure(state="disabled")
  157.             self._looking_for_1_half = False
  158.             self._looking_for_2_half = False
  159.             self._reviewing = False
  160.             for i in self._data_widgets:
  161.                 i.destroy()
  162.  
  163.     def _open_camera(self, queue):
  164.         ret, frame = self.cap.read()
  165.         if not self._reviewing:
  166.  
  167.             if ret:
  168.                 for d in decode(frame, symbols=[ZBarSymbol.QRCODE]):
  169.                     s = d.data.decode()
  170.                     if s[:3] == "<1>" and not self._looking:
  171.                         self._looking = True
  172.                         self._looking_for_2_half = True
  173.                         self._temp_data = s[3:]
  174.                         debug("Looking for 2nd qr code")
  175.                     elif s[:3] == "<2>" and not self._looking:
  176.                         self._looking = True
  177.                         self._looking_for_1_half = True
  178.                         self._temp_data = s[3:]
  179.                         debug("Looking for 1nd qr code")
  180.                     elif s[:3] == "<1>" and self._looking_for_1_half:
  181.                         p1s = s[3:]
  182.                         p2s = self._temp_data
  183.                         fs = p1s + p2s
  184.                         self._temp_data = fs
  185.                         debug("found first half")
  186.                         debug(self._temp_data)
  187.                         self._reviewing = True
  188.                         self._looking_for_1_half = False
  189.                         self._looking_for_2_half = False
  190.                         InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
  191.                     elif s[:3] == "<2>" and self._looking_for_2_half:
  192.                         p1s = self._temp_data
  193.                         p2s = s[3:]
  194.                         fs = p1s + p2s
  195.                         self._temp_data = fs
  196.                         debug("found second half")
  197.                         debug(self._temp_data)
  198.                         self._reviewing = True
  199.                         self._looking_for_1_half = False
  200.                         self._looking_for_2_half = False
  201.                         InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
  202.                     elif (not s[:3] == "<2>") and (not s[:3] == "<1>"):
  203.                         debug("have all data")
  204.                         self._reviewing = True
  205.                         self._temp_data = s
  206.                         debug(self._temp_data)
  207.                         InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
  208.  
  209.                     frame = cv2.rectangle(frame, (d.rect.left, d.rect.top),
  210.                                           (d.rect.left + d.rect.width, d.rect.top + d.rect.height), (0, 255, 0), 3)
  211.             if self._looking_for_1_half:
  212.                 frame = cv2.putText(frame, "Show First QR", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
  213.                                     (0, 0, 255), 2, cv2.LINE_AA)
  214.             elif self._looking_for_2_half:
  215.                 frame = cv2.putText(frame, "Show Second QR", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
  216.                                     (0, 0, 255), 2, cv2.LINE_AA)
  217.         else:
  218.             frame = cv2.putText(frame, "Reviewing", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
  219.                                 (0, 0, 255), 2, cv2.LINE_AA)
  220.         opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
  221.         queue.put(opencv_image)
  222.  
  223.     # self.vid.after(10, self._open_camera)
  224.     def _team_data_packet_to_list(self, team_data_packet: str) -> list:
  225.         team_data: list = [{
  226.             "Data type": team_data_packet[:2]
  227.         }]
  228.         # get teamData out of packet
  229.         team_data_combined = re.findall(
  230.             "((([a-zA-Z0-9\s]+):(\d+|\"[a-zA-Z0-9\s.\-']+\"|t|f|[a-zA-Z0-9\s\-])+;)+)>",
  231.             team_data_packet)
  232.         # convert teamData to dict
  233.         for i in team_data_combined:
  234.             temp_data = dict()
  235.             for ii in re.findall("([a-zA-Z0-9\s]+):(\"[a-zA-Z\s0-9.'-]+\"|t|f|[0-9\-]+|[a-zA-Z\s]+);", i[0]):
  236.                 if ii[1].strip("-").isdigit():
  237.                     temp_data[ii[0]] = int(ii[1])
  238.                 elif ii[1] == "t":
  239.                     temp_data[ii[0]] = True
  240.                 elif ii[1] == "f":
  241.                     temp_data[ii[0]] = False
  242.                 else:
  243.                     temp_data[ii[0]] = ii[1].strip("\"")
  244.             team_data.append(temp_data)
  245.  
  246.         for i in team_data:
  247.             debug(i)
  248.         debug(f"length of team_data:{len(team_data)}")
  249.         self.send_button.configure(state="normal")
  250.         self._update_list = True
  251.         return team_data
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement