Advertisement
edsheut

Object Capture - video stream imutils

Oct 17th, 2020 (edited)
2,643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. # Importar los paquetes necesarios
  2. from picamera.array import PiRGBArray
  3. from picamera import PiCamera
  4. import time
  5. import cv2
  6. import numpy as np
  7. from imutils.video import VideoStream
  8. import imutils
  9.  
  10. # Para calcular el tiempo de recalentamiento
  11. t0 = time.clock()
  12.  
  13. # Inicializar la camara y configurar la captura RAW
  14. vs = VideoStream(src=0).start()
  15.  
  16. # Calentamiento de la camara
  17. time.sleep(2.0)
  18.  
  19. # Seleccionar los rangos de detección
  20. lower_range = np.array([29, 86, 30], dtype=np.uint8)
  21. upper_range = np.array([64, 255, 255], dtype=np.uint8)
  22.  
  23. # Capturar imágenes de la cámara
  24. while True:
  25.     # capturar imagen
  26.     frame = vs.read()
  27.  
  28.     # Preprocesar
  29.     frame = imutils.resize(frame, width=600)
  30.     blurred = cv2.GaussianBlur(frame, (11, 11), 0)
  31.     hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
  32.  
  33.     # mascara
  34.     mask = cv2.inRange(hsv, lower_range, upper_range)
  35.     mask = cv2.erode(mask, None, iterations=2)
  36.     mask = cv2.dilate(mask, None, iterations=2)
  37.    
  38.     # buscar contornos
  39.     cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  40.     cnts = imutils.grab_contours(cnts)
  41.     center = None
  42.  
  43.     # si se encuentran contornos
  44.     if len(cnts) > 0:
  45.         # buscar el contorno mas grande y calcular el centroide y el circulo mas pequeño alrededor
  46.         c = max(cnts, key=cv2.contourArea)
  47.         ((x, y), radius) = cv2.minEnclosingCircle(c)
  48.         M = cv2.moments(c)
  49.         center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  50.  
  51.         if radius > 10:
  52.             # dibujar circulo
  53.             cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
  54.             cv2.circle(frame, center, 5, (0, 0, 255), -1)
  55.  
  56.     # mostrar la imagen
  57.     cv2.imshow("Frame", frame)
  58.     key = cv2.waitKey(1) & 0xFF
  59.    
  60.     # Sí se presiona ´q´ salir
  61.     if key == ord("q"):
  62.         break
  63. # Detener el videoStream
  64. vs.stop()
  65. cv2.destroyAllWindows()
  66.  
  67. # calcular el tiempo
  68. t1 = time.clock() - t0
  69. print("Transcurrido hasta sobrecalentamiento: ", t1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement