Advertisement
edsheut

Object capture - Raw

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