Skip to content

Commit

Permalink
Realtime Detection Using Opencv (qqwweee#5)
Browse files Browse the repository at this point in the history
* Add Realtime Detection Using Opencv
  • Loading branch information
tanakataiki authored and qqwweee committed Apr 6, 2018
1 parent bd72721 commit 2c320ef
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import os
import random
import time

import cv2
import numpy as np
from keras import backend as K
from keras.models import load_model
from PIL import Image, ImageDraw, ImageFont
from timeit import time
from timeit import default_timer as timer ### to calculate FPS

from yolo3.model import yolo_eval

Expand Down Expand Up @@ -71,10 +73,10 @@ def generate(self):

def detect_image(self, image):
start = time.time()

resized_image = image.resize(tuple(reversed(self.model_image_size)), Image.BICUBIC)
image_data = np.array(resized_image, dtype='float32')

print(image_data.shape)
image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.

Expand Down Expand Up @@ -131,12 +133,34 @@ def close_session(self):
self.sess.close()

def detect_video(yolo):
from VideoCapture import Device
camera = Device()
vid = cv2.VideoCapture(0) ### TODO: will video path other than 0 be used?
if not vid.isOpened():
raise IOError("Couldn't open webcam")
accum_time = 0
curr_fps = 0
fps = "FPS: ??"
prev_time = timer()
while True:
frame = camera.getImage()
image = yolo.detect_image(frame)
image.show()
return_value, frame = vid.read()
image = Image.fromarray(frame)
image = yolo.detect_image(image)
result = np.asarray(image)

curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50,
color=(255, 0, 0), thickness=2)

cv2.imshow("result",result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
yolo.close_session()


Expand All @@ -156,5 +180,5 @@ def detect_img(yolo):

if __name__ == '__main__':
yolo = YOLO()
detect_img(yolo)
#detect_video(yolo)
#detect_img(yolo)
detect_video(yolo)

0 comments on commit 2c320ef

Please sign in to comment.