如何在opencv python中绘制边界框?

时间:2018-05-19 04:51:37

标签: python opencv

我为猫狗制作了一个图像分类器,我已经对它进行了实时图像分类。现在,它没有在边界框上显示图像。如何让狗或猫的图像出现在边界上框。   这是代码

from keras.preprocessing.image import img_to_array
import cv2
import time
from keras.models import load_model
from imutils.video import VideoStream
import imutils
import numpy as np
import os
from keras.preprocessing.image import ImageDataGenerator

conc =0
print("Loading model....")
model=load_model('model_after_trained.h5')
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=     ['accuracy'])

 print("starting video stream.......")
 vs=VideoStream(src=0).start()
 time.sleep(2)
 proba=9
 while True:
  frame=vs.read()
  frame=imutils.resize(frame,width=400)

  image = ImageDataGenerator(rescale = 1./255,
  shear_range = 0.2,
  zoom_range = 0.2,
  horizontal_flip = True)

  image = cv2.resize(frame, (64, 64))

  image = img_to_array(image)


 image = np.expand_dims(image, axis=0)
 k=model.predict(image)[0]

  if k==1:
   label1='dog'
   conc +=1
   label = "{}: {:.2f}%".format(label1, proba * 100)
   frame = cv2.putText(frame, label, (10, 25),cv2.FONT_HERSHEY_SIMPLEX,   0.7, (0, 255, 0), 2)
   cv2.imshow("Frame", frame)
   key = cv2.waitKey(1) & 0xFF
else:

 label='cat'
 conc=0
 label = "{}: {:.2f}%".format(label, proba * 100)
 frame = cv2.putText(frame, label, (10, 25),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
 cv2.imshow("Frame", frame)
 = cv2.waitKey(1) & 0xFF

if key==ord('q'):
 break
 vs.stop()

提前致谢!

0 个答案:

没有答案