Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
import cv2
import numpy as np
import time
from time import sleep
import datetime as dt
cap = cv2.VideoCapture(0)
prototxt = 'models/solver.prototxt'
model = 'models/squeezenet_v1.1.caffemodel'
labels = 'models/synset_words.txt'
rows = open(labels).read().strip().split("\n")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
#
net = cv2.dnn.readNetFromCaffe(prototxt, model)
# reset FPS meter
t0 = time.time()
while(True):
# take each frame
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# only for the colors:
lower_blue = np.array([0,0,0])
upper_blue = np.array([255,255,255])
# threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# putting frame into blob
res = cv2.bitwise_and(frame,frame, mask= mask)
blob = cv2.dnn.blobFromImage(res, 1, (224, 224), (104, 117, 124))
# measuring the FPS
FPS = 1/(time.time() - t0)
t0 = time.time()
# trying to find the object
net.setInput(blob)
preds = net.forward()
preds = preds.reshape((1, len(classes)))
idxs = int(np.argsort(preds[0])[::-1][:1])
# creating text for label
text = "Label: {}, p = {:.2f}%, fps = {:.2f}".format(classes[idxs], preds[0][idxs] * 100, FPS)
# putting text on frame
cv2.putText(res, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# prewiev
cv2.imshow('res',res)
# saving results 1-image for ~2.5 secconds
# fname = 'pic_' + dt.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '.jpg'
# cv2.imwrite(fname, res)
# exit from frame capturing
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
Котики vs нейросеть 2. Или запускаем SqueezeNet v.1.1 на Raspberry Zero в realtime (почти)