Sunteți pe pagina 1din 6

import RPi.

GPIO as GPIO

import cv2

import numpy as np

from movimiento import *

GPIO.setmode(GPIO.BCM)

GPIO.setup(26,GPIO.OUT)

GPIO.setup(14,GPIO.OUT)

ancho = 640

largo = 480

centrox = ancho/2

minArea = 50

class VideoCamera(object):

def __init__(cap, resolution=(ancho, largo)):

# Using OpenCV to capture from device 0. If you have trouble capturing

# from a webcam, comment the line below out and use a video file

# instead.

cap.video = cv2.VideoCapture(0)

kernel=np.ones((5,5),np.uint8)

cap.video.set(cv2.CAP_PROP_FRAME_WIDTH, ancho)
cap.video.set(cv2.CAP_PROP_FRAME_HEIGHT, largo)

# If you decide to use video.mp4, you must have this file in the folder

# as the main.py.

# self.video = cv2.VideoCapture('video.mp4')

def __del__(cap):

cap.video.release()

def get_frame(cap):

success, image = cap.video.read()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

h,w,m=image.shape

kernel=np.ones((5,5),np.uint8)

# kernel=np.ones((3,3),np.uint8)

#amarillo

colorLower = (28,100,100)

colorUpper = (38,255,255)

mask = cv2.inRange(hsv, colorLower, colorUpper)


res = cv2.bitwise_and(image, image, mask =mask)

moments = cv2.moments(mask)

area=int(moments['m00'])

f = open ('coordenadas.txt','w')

f.write ('area')

f.close

x=0

y=0

dist=0

# print (area)

if(area > 100000):

#Buscamos el centro x, y del objeto

x = int(moments['m10']/moments['m00'])

y = int(moments['m01']/moments['m00'])

#se realizan calculos para definir la distancia respecto al area

if area>=845695.7:

if area>=2301333.5:

dist=-(area/307781.175)+37.4771743

if area<2301333.5:

if area>=859737.4:
dist=-(area/72079.83)+61.9275725

if area<859737.4:

dist=-(area/19652.8425)+93.74622211

if int(dist)<=15:

dist=15

if int(dist)>=70:

dist=70

#Mostramos sus coordenadas y distancia de la pelota

# print ("Coordenadas, Area: ", (x-w/2),(-(y-h/2)),int(dist))

print ("Coordenadas, Area: ", (x-w/2),int(dist))

if (x-w/2) == 0:

paro()

# if (x-w/2) >=1 or (x-w/2) <=-1:

# adelante()

# else:

# paro()

#Giro derecha

# if (x-w/2) >=160 and (x-w/2) <=310:

# print ('mas de 160')

# GPIO.output(14,True)

# derecha()

# else:

# print ('menos de 160')

# GPIO.output(14,False)

# adelante()
#Giro izquierda

# if (x-w/2) <=-130 and (x-w/2) >=-280:

# print ('mas de 160')

# GPIO.output(26,True)

# izquierda()

# else:

# print ('menos de 160')

# GPIO.output(26,False)

#Divide la imagen en cuatro cuadrantes

# cv2.line(image,(0,h//2),(w,h//2),(150,200,0),2)

# cv2.line(image,(w//2,0),(w//2,h),(150,200,0),2)

#Definir centro pelota

opening=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

x,y,w,h=cv2.boundingRect(opening)

cv2.circle(image,(x+w//2,y+h//2),5,(0,0,255),-1)

# We are using Motion JPEG, but OpenCV defaults to capture raw images,

# so we must encode it into JPEG in order to correctly display the

# video stream.

ret, jpeg = cv2.imencode('.jpg',image)

ret, jpeg = cv2.imencode('.jpg',res)

return jpeg.tobytes()

S-ar putea să vă placă și