Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import time
  2. import tkinter as tk
  3. import settings
  4. from PIL import Image, ImageTk
  5. import imutils
  6. from pydarknet import Detector, Image
  7. import cv2
  8.  
  9. def start_video():
  10.     settings.start_video = True
  11.     show_frame()
  12.  
  13. def stop_video():
  14.     settings.start_video = False
  15.     settings.start_processing = False
  16.     lmain.config(image='')
  17.  
  18. def start_process():
  19.     settings.start_processing = True
  20.  
  21. def stop_process():
  22.     settings.start_processing = False
  23.  
  24. def show_frame():
  25.     if not settings.start_video:
  26.         return None
  27.  
  28.     _, frame = cap.read()
  29.     frame = imutils.resize(frame, width=400)
  30.  
  31.     if settings.start_processing:
  32.         frame = process_frame(frame)
  33.  
  34.     cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  35.     img = Image.fromarray(cv2image)
  36.     imgtk = ImageTk.PhotoImage(image=cv2image)
  37.     lmain.imgtk = imgtk
  38.     lmain.configure(image=imgtk)
  39.     lmain.after(10, show_frame)
  40.  
  41. def process_frame(img):
  42.     # grab the frame dimensions and convert it to a blob
  43.     (h, w) = img.shape[:2]
  44.     blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)),
  45.                                  0.007843, (300, 300), 127.5)
  46.  
  47.     # pass the blob through the network and obtain the detections and
  48.     # predictions
  49.     #net.setInput(blob)
  50.     #detections = net.forward()
  51.  
  52.     start_time = time.time()
  53.  
  54.     # Only measure the time taken by YOLO and API Call overhead
  55.  
  56.     dark_frame = Image(img)
  57.     results = net.detect(dark_frame)
  58.     del dark_frame
  59.  
  60.     end_time = time.time()
  61.     print("Elapsed Time:",end_time-start_time)
  62.  
  63.     for cat, score, bounds in results:
  64.         x, y, w, h = bounds
  65.         cv2.rectangle(img, (int(x-w/2),int(y-h/2)),(int(x+w/2),int(y+h/2)),(255,0,0))
  66.         cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))
  67.  
  68.     #cv2.imshow("preview", frame)
  69.  
  70. # load our serialized model from disk
  71. print("Loading model...")
  72. net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0,
  73.                    bytes("cfg/coco.data", encoding="utf-8"))
  74. cap = cv2.VideoCapture(0)
  75.  
  76. window = tk.Tk()
  77. window.title("DEMO")
  78. window.geometry('700x420')
  79. lbl = tk.Label(window, text="Smart Machine Vision System", font=("Arial Bold", 24))
  80. lbl.grid(column=1, row=0)
  81. imageFrame = tk.Frame(window, width=600, height=500)
  82. imageFrame.grid(row=1, column=1, padx=10, pady=2)
  83. lmain = tk.Label(imageFrame, text="Press Start Video")
  84. lmain.grid(row=1, column=1)
  85. startVideoStreamBtn = tk.Button(window, text="Start Video", command=start_video)
  86. startVideoStreamBtn.grid(column=0, row=2, padx=15)
  87. stopVideoStreamBtn = tk.Button(window, text="Stop Video", command=stop_video)
  88. stopVideoStreamBtn.grid(column=0, row=3, padx=15)
  89. startProcessBtn = tk.Button(window, text="Start Detection", command=start_process)
  90. startProcessBtn.grid(column=1, row=2)
  91. stopProcessBtn = tk.Button(window, text="Stop Detection", command=stop_process)
  92. stopProcessBtn.grid(column=1, row=3)
  93. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement