Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import tkinter as tk
- import settings
- from PIL import Image, ImageTk
- import imutils
- from pydarknet import Detector, Image
- import cv2
- def start_video():
- settings.start_video = True
- show_frame()
- def stop_video():
- settings.start_video = False
- settings.start_processing = False
- lmain.config(image='')
- def start_process():
- settings.start_processing = True
- def stop_process():
- settings.start_processing = False
- def show_frame():
- if not settings.start_video:
- return None
- _, frame = cap.read()
- frame = imutils.resize(frame, width=400)
- if settings.start_processing:
- frame = process_frame(frame)
- cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
- img = Image.fromarray(cv2image)
- imgtk = ImageTk.PhotoImage(image=cv2image)
- lmain.imgtk = imgtk
- lmain.configure(image=imgtk)
- lmain.after(10, show_frame)
- def process_frame(img):
- # grab the frame dimensions and convert it to a blob
- (h, w) = img.shape[:2]
- blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)),
- 0.007843, (300, 300), 127.5)
- # pass the blob through the network and obtain the detections and
- # predictions
- #net.setInput(blob)
- #detections = net.forward()
- start_time = time.time()
- # Only measure the time taken by YOLO and API Call overhead
- dark_frame = Image(img)
- results = net.detect(dark_frame)
- del dark_frame
- end_time = time.time()
- print("Elapsed Time:",end_time-start_time)
- for cat, score, bounds in results:
- x, y, w, h = bounds
- cv2.rectangle(img, (int(x-w/2),int(y-h/2)),(int(x+w/2),int(y+h/2)),(255,0,0))
- cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))
- #cv2.imshow("preview", frame)
- # load our serialized model from disk
- print("Loading model...")
- net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0,
- bytes("cfg/coco.data", encoding="utf-8"))
- cap = cv2.VideoCapture(0)
- window = tk.Tk()
- window.title("DEMO")
- window.geometry('700x420')
- lbl = tk.Label(window, text="Smart Machine Vision System", font=("Arial Bold", 24))
- lbl.grid(column=1, row=0)
- imageFrame = tk.Frame(window, width=600, height=500)
- imageFrame.grid(row=1, column=1, padx=10, pady=2)
- lmain = tk.Label(imageFrame, text="Press Start Video")
- lmain.grid(row=1, column=1)
- startVideoStreamBtn = tk.Button(window, text="Start Video", command=start_video)
- startVideoStreamBtn.grid(column=0, row=2, padx=15)
- stopVideoStreamBtn = tk.Button(window, text="Stop Video", command=stop_video)
- stopVideoStreamBtn.grid(column=0, row=3, padx=15)
- startProcessBtn = tk.Button(window, text="Start Detection", command=start_process)
- startProcessBtn.grid(column=1, row=2)
- stopProcessBtn = tk.Button(window, text="Stop Detection", command=stop_process)
- stopProcessBtn.grid(column=1, row=3)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement