Commit 15e47db7 authored by William Michalski's avatar William Michalski
Browse files

main.py

parent 553b2ede
# Credit: Adrian Rosebrock
# https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/
# import the necessary packages
from picamera.array import PiRGBArray # Generates a 3D RGB array
from picamera import PiCamera # Provides a Python interface for the RPi Camera Module
import time # Provides time-related functions
import cv2 # OpenCV library
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from math import sqrt
def centroid(img):
M = cv2.moments(img)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
return cX, cY
red_lower = np.array([136, 50, 50], np.uint8)
red_upper = np.array([180, 255, 255], np.uint8)
res 480
rate = 40#FPS
fichier = open("mesure_tps_par_resolution.txt", "a")
# Initialize the camera
camera = PiCamera()
# Set the camera resolution
camera.resolution = (res,res)
# Set the number of frames per second
camera.framerate = rate
# Generates a 3D RGB array and stores it in rawCapture
raw_capture = PiRGBArray(camera, size=(res,res))
# Wait a certain number of seconds to allow the camera time to warmup
time.sleep(2)
aux = 0
time_mean = 0
time_var = 0
loaded = False
exec_time = 0
exec_begin = time.time()
# Capture frames continuously from the camera
for frame in camera.capture_continuous(raw_capture, format="bgr", use_video_port=True):
# Grab the raw NumPy array representing the image
img = frame.array
#A decommenter pour mesurer avec le temps de detection de la bille
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)#Espace HSV, plus simple de separer les couleurs
img_red = cv2.inRange(img_hsv, red_lower, red_upper)
rX,rY = centroid(img_red)
cv2.circle(img, (rX,rY), 4, (0,233,255),-1)#Ajout d'un point sur l'image d'origine, pour montrer ou ce trouve le centre detecte
A decommenter pour voir la video, mais penser que ça fausse la vitesse d'execution
cv2.imshow("Frame", img)
# Wait for keyPress for 1 millisecond
key = cv2.waitKey(1) & 0xFF
# Clear the stream in preparation for the next frame
raw_capture.truncate(0)
# If the `q` key was pressed, break from the loop
exec_time = time.time() - exec_begin
if key == ord("q") or exec_time >= limit_time:
break
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment