1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| # Imports
import picamera
import time
import os
import numpy as np
import cv2
# Supression de l'image précédente dans le fichier
file = os.listdir('/home/pi/Camera/PhotoReconnaissance')
os.remove('/home/pi/Camera/PhotoReconnaissance'+'/'+file[0])
camera = picamera.PiCamera()
#camera.resolution = (720,1080)
camera.start_preview(fullscreen = False, window=(50,50,640,480))
time.sleep(5)
# Photo prise
camera.capture('/home/pi/Camera/PhotoReconnaissance/image.bmp')
camera.stop_preview()
# Récpération de l'image prise
image = cv2.imread('/home/pi/Camera/PhotoReconnaissance/image.bmp',0)
gray = image
ret,thresh = cv2.threshold(gray,250,255,cv2.THRESH_BINARY_INV)
img,contours,h =cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
perimetre=cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.01*perimetre,True)
M = cv2.moments(cnt)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.drawContours(image,[cnt],-1,(0,255,0),2)
if len(approx)==3:
shape = "triangle"
elif len(approx)==4:
(x, y, w, h) = cv2.boundingRect(approx)
ratio = w / float(h)
if ratio >= 0.70 and ratio <= 1.30:
shape = "carre"
else:
shape = "rectangle"
elif len(approx)==5:
shape = "pentagone"
elif len(approx)==6:
shape = "hexagone"
else:
shape= "circle"
cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 2)
image_data = np.asarray(image)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows() |
Partager