Skip to content

Instantly share code, notes, and snippets.

@theSekyi
Created April 16, 2019 11:26
Show Gist options
  • Select an option

  • Save theSekyi/a336dd939bc0819cbf021d2a624b414b to your computer and use it in GitHub Desktop.

Select an option

Save theSekyi/a336dd939bc0819cbf021d2a624b414b to your computer and use it in GitHub Desktop.
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os
# load model from disk
print('Loading model..')
net = cv2.dnn.readNetFromCaffe('model_weights/deploy.prototxt.txt',
'model_architecture/res10_300x300_ssd_iter_140000.caffemodel')
confidence_d = 0.5
# Initialize video stream
print('Starting video stream')
vs = VideoStream(src=0).start()
time.sleep(1.0)
total = 0
name = input('What is your name: ')
destination = f"./dataset/{name}"
#check existence of folder
def does_folder_exist():
pass
# Keep frame open to capture images
while True:
#Grab frame and resize
frame = vs.read()
orig = frame.copy()
frame = imutils.resize(frame,width=400)
#grab frame dimensions and convert it to a blob
(h,w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame,(300,300)),1.0,
(300,300),(104.0,177.0,123.0))
#pass the blob through the network and obtain predictions
net.setInput(blob)
detections = net.forward()
print(detections)
#loop over the detections
for i in range(0, detections.shape[2]):
#extract confidence
confidence = detections[0,0,i,2]
#filter out weak detections by ensuring the confidence
#greater than minimum confidence
if confidence < confidence_d:
continue
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
text = f"{confidence *100}"
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
#show output frame
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
#if the q key was pressed, break from the loop
if key == ord(' '):
# if os.path.isdir("dataset"):
# pass
# else:
# os.mkdir('dataset')
p = os.path.sep.join([destination,f"{name}_{str(total).zfill(5)}.png"])
cv2.imwrite(p, orig)
total += 1
print(f"Captured {total} image(s)")
elif key == ord("q"):
print("quiting program")
break
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment