Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from customtkinter import CTk
- from customtkinter import CTkCanvas as Canvas, CTkEntry as Entry, CTkTextbox as Textbox, CTkButton as Button, \
- CTkLabel as Label, CTkTabview as Tabview, CTkFrame as Frame, CTkScrollableFrame as ScrollableFrame, \
- CTkCheckBox as CheckBox, CTkImage
- from app.views.classes import CTkMyTabview as MyTabView, CTkMyScrollableFrame as myscrollableframe
- from app.views.classes import data, InternetManager
- import cv2
- from pyzbar.pyzbar import decode, ZBarSymbol
- from PIL import Image, ImageTk
- from logging import debug, info, warning, error, critical
- import re
- from multiprocessing import Process, Queue
- camera_id = 0
- delay = 1
- testing = data.testing
- class DataInputScreen:
- def __init__(self, tab: Frame, root: CTk, tv: MyTabView):
- self.tv = tv
- self.tab = tab
- self.root = root
- self.cap: cv2.VideoCapture = None
- self.menu = Frame(self.tab)
- self.vid_frame = Frame(self.tab)
- self.vid = Label(self.vid_frame, text="")
- self.vid.pack(side='right', expand=True, padx=5, pady=5)
- self.menu.place(x=0, y=0, relwidth=.4, relheight=1)
- self.vid_frame.place(relx=0.4, y=0, relwidth=.6, relheight=1)
- # if not testing:
- # self.cap = cv2.VideoCapture(camera_id)
- # self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1000)
- # self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1000)
- # Menu Setup
- self.scrollabaleframe = myscrollableframe(self.menu, fg_color=data.lightBackgroudColor, corner_radius=30)
- self.scrollabaleframe.pack(expand=True, fill="both", padx=10, pady=10)
- self.send_button = Button(self.menu, text="Send", height=60, corner_radius=400, command=self._send)
- self.send_button.configure(state="disabled")
- self.send_button.cget("font").configure(size=46)
- self.send_button.pack(fill="x", padx=10, pady=(0, 10))
- self._previous_tab = ""
- self._temp_data: str = ""
- self._data_widgets = []
- self._reviewing = False
- self._looking_for_1_half = False
- self._looking_for_2_half = False
- self._looking = False
- self._update_list = False
- self._vcmd = (self.root.register(self._validate), "%P")
- if testing:
- test_img = CTkImage(light_image=Image.open("app/res/test/test1080p.png"), size=(1920, 1080))
- self.vid.photo_image = test_img
- self.vid.configure(image=test_img)
- Button(self.vid, text="Extend _Data", command=self._refresh, corner_radius=1000, height=50).place(relx=.35, rely=.475, relwidth=.3)
- self._update_list = True
- self.q = Queue()
- # Create a Process that will execute the capture_frames function
- Process(target=self.empty, args=(self.q,)).start()
- def empty(self):
- pass
- def _refresh(self):
- InternetManager.data.extend(self._team_data_packet_to_list(data.temp_team_data))
- self._update_list = True
- def loop(self):
- # if not testing:
- # self._open_camera()
- if self.q.empty():
- opencv_image = self.q.get()
- captured_image = Image.fromarray(opencv_image)
- photo_image = ImageTk.PhotoImage(image=captured_image)
- self.vid.photo_image = photo_image
- self.vid.configure(image=photo_image)
- self._menu_loop()
- def _cam_loop(self, queue):
- # if not self.tv.get() == self._previous_tab:
- self.cap = cv2.VideoCapture(camera_id)
- self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1000)
- self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1000)
- # if self.tv.get() == "Data Input":
- while True:
- print("sdakjdbjlosa")
- # self._open_camera(queue)
- # else:
- # self.cap.release()
- # self._previous_tab = self.tv.get()
- def _menu_loop(self):
- if self._update_list:
- for i in InternetManager.data:
- boxframe = Frame(self.scrollabaleframe, fg_color=data.backgroundColor,
- bg_color=data.lightBackgroudColor, corner_radius=30)
- boxframe.pack(expand=True, fill="x", padx=1, pady=5)
- self._data_widgets.append(boxframe)
- for x, y in i.items():
- if type(y) == int:
- f = Frame(boxframe)
- f.pack()
- Label(f, text=x + ": ").pack(side="left", padx=(15, 0), pady=7)
- t = Entry(f, placeholder_text="Int", validate="key", validatecommand=self._vcmd, width=100)
- t.insert(0, str(y))
- t.configure(state="disabled")
- t.pack(side="left")
- elif type(y) == bool:
- if y:
- t = CheckBox(boxframe, text=x, onvalue="True", offvalue="False")
- t.select()
- t.configure(state="disabled")
- t.pack()
- else:
- t = CheckBox(boxframe, text=x, onvalue="True", offvalue="False")
- t.deselect()
- t.configure(state="disabled")
- t.pack()
- elif type(y) == str:
- f = Frame(boxframe)
- f.pack()
- Label(f, text=x + ": ").pack(side="left", padx=(15, 0), pady=7)
- t = Entry(f, placeholder_text="Text", width=200)
- t.insert(0, y)
- t.configure(state="disabled")
- t.pack(side="left")
- self._update_list = False
- def _validate(self, val):
- if re.fullmatch("^[-]?\d+$", val) is None:
- return False
- return True
- def _send(self):
- InternetManager.upload()
- if not data.testing:
- self._update_list = True
- self.send_button.configure(state="disabled")
- self._looking_for_1_half = False
- self._looking_for_2_half = False
- self._reviewing = False
- for i in self._data_widgets:
- i.destroy()
- def _open_camera(self, queue):
- ret, frame = self.cap.read()
- if not self._reviewing:
- if ret:
- for d in decode(frame, symbols=[ZBarSymbol.QRCODE]):
- s = d.data.decode()
- if s[:3] == "<1>" and not self._looking:
- self._looking = True
- self._looking_for_2_half = True
- self._temp_data = s[3:]
- debug("Looking for 2nd qr code")
- elif s[:3] == "<2>" and not self._looking:
- self._looking = True
- self._looking_for_1_half = True
- self._temp_data = s[3:]
- debug("Looking for 1nd qr code")
- elif s[:3] == "<1>" and self._looking_for_1_half:
- p1s = s[3:]
- p2s = self._temp_data
- fs = p1s + p2s
- self._temp_data = fs
- debug("found first half")
- debug(self._temp_data)
- self._reviewing = True
- self._looking_for_1_half = False
- self._looking_for_2_half = False
- InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
- elif s[:3] == "<2>" and self._looking_for_2_half:
- p1s = self._temp_data
- p2s = s[3:]
- fs = p1s + p2s
- self._temp_data = fs
- debug("found second half")
- debug(self._temp_data)
- self._reviewing = True
- self._looking_for_1_half = False
- self._looking_for_2_half = False
- InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
- elif (not s[:3] == "<2>") and (not s[:3] == "<1>"):
- debug("have all data")
- self._reviewing = True
- self._temp_data = s
- debug(self._temp_data)
- InternetManager.data.extend(self._team_data_packet_to_list(self._temp_data))
- frame = cv2.rectangle(frame, (d.rect.left, d.rect.top),
- (d.rect.left + d.rect.width, d.rect.top + d.rect.height), (0, 255, 0), 3)
- if self._looking_for_1_half:
- frame = cv2.putText(frame, "Show First QR", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
- (0, 0, 255), 2, cv2.LINE_AA)
- elif self._looking_for_2_half:
- frame = cv2.putText(frame, "Show Second QR", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
- (0, 0, 255), 2, cv2.LINE_AA)
- else:
- frame = cv2.putText(frame, "Reviewing", (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 2,
- (0, 0, 255), 2, cv2.LINE_AA)
- opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
- queue.put(opencv_image)
- # self.vid.after(10, self._open_camera)
- def _team_data_packet_to_list(self, team_data_packet: str) -> list:
- team_data: list = [{
- "Data type": team_data_packet[:2]
- }]
- # get teamData out of packet
- team_data_combined = re.findall(
- "((([a-zA-Z0-9\s]+):(\d+|\"[a-zA-Z0-9\s.\-']+\"|t|f|[a-zA-Z0-9\s\-])+;)+)>",
- team_data_packet)
- # convert teamData to dict
- for i in team_data_combined:
- temp_data = dict()
- 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]):
- if ii[1].strip("-").isdigit():
- temp_data[ii[0]] = int(ii[1])
- elif ii[1] == "t":
- temp_data[ii[0]] = True
- elif ii[1] == "f":
- temp_data[ii[0]] = False
- else:
- temp_data[ii[0]] = ii[1].strip("\"")
- team_data.append(temp_data)
- for i in team_data:
- debug(i)
- debug(f"length of team_data:{len(team_data)}")
- self.send_button.configure(state="normal")
- self._update_list = True
- return team_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement