Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from keras.applications import ResNet50
  4. from keras.applications import imagenet_utils
  5. from keras.preprocessing.image import img_to_array
  6.  
  7.  
  8. model = ResNet50(weights='imagenet')
  9. camera = cv2.VideoCapture(0)
  10. # If possible control the FPS to constrain the labels appearances
  11. # camera.set(cv2.CAP_PROP_FPS, 4)
  12.  
  13. while True:
  14. (grabbed, frame) = camera.read()
  15. # frame = imutils.resize(frame, width=256)
  16.  
  17. image = cv2.resize(frame, (224, 224))
  18. image = img_to_array(image)
  19. image = imagenet_utils.preprocess_input(image)
  20. image = np.expand_dims(image, axis=0)
  21.  
  22. preds = model.predict(image)
  23. P = imagenet_utils.decode_predictions(preds)[0]
  24.  
  25. for i in range(3):
  26. prob = str(np.round(P[i][2], 2))
  27. cv2.putText(frame, "{} : {}".format(P[i][1], prob), (10, 30*(i+1)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
  28. cv2.imshow("Classification", frame)
  29.  
  30. if cv2.waitKey(1) & 0xFF == ord("q"):
  31. break
  32.  
  33. camera.release()
  34. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement