Crozz Bite
Posts: 14
Joined: Thu Jan 24, 2019 5:23 pm

cv2 camara problem (no img)

Tue Apr 16, 2019 9:36 pm

Hi everyone ,im using cv2 library with the camera, the hardware works fine but i dont know why the camara stop the frame ,

I am realy new ,my progam is going to folow a line and the carmara is going to check out the color of a line and control 2 pwm pulses to control some moves
if separate the camera and also the logic both works good but when i try to convine the code the camara stop to work so my pwms dont work fine to there is the code the coments are in spanish but i think is easy to know whats goin on ,

Code: Select all

import numpy as np
import cv2
from time import sleep
import RPi.GPIO as GPIO
#Variables
#PINES_Name-----------------
Motor_A = 37
Motor_B = 40
Dir_A = 18
Dir_B = 16
Defensa=3#
Start=36#talves cambien los pines
travajo=0
#x=0
#y=0
tiempo=0
#-------Funciones---------------
#Configura los pines de la RPi a pines fisicos
GPIO.setmode(GPIO.BOARD)
#Pines_Salida
GPIO.setup(Motor_A,GPIO.OUT)
GPIO.setup(Motor_B,GPIO.OUT)
GPIO.setup(Dir_A,GPIO.OUT)
GPIO.setup(Dir_B,GPIO.OUT)
GPIO.output(Dir_B,True)
GPIO.output(Dir_A,True)
#PWM delaracion  
PWM_A = GPIO.PWM(Motor_A,100)
PWM_B = GPIO.PWM(Motor_B,100)
#Pines_Entrada
#Pull up para que esten siempre en 3.3 y si se preciona van tierra
GPIO.setup(Start,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(Defensa,GPIO.IN,pull_up_down=GPIO.PUD_UP)
cap=cv2.VideoCapture(0)
kernel=np.ones((5,5),np.uint8)

    
    
#Funcion para que la camara funcione
def Camara():
    camara=1
    while (camara==1):
        ret, frame = cap.read()
#Rango de colores para la linea (cinta azul max[255,130,80]min[170,80,50])
        rangomax=np.array([255,130,80])
        rangomin=np.array([170,80,50])
        mascara=cv2.inRange(frame,rangomin,rangomax)
        opening=cv2.morphologyEx(mascara,cv2.MORPH_OPEN,kernel)  
        x,y,w,h=cv2.boundingRect(opening )   
#Rectangulo de vicion 
        cv2.rectangle(frame, (x,y),(int((x+w)/2),int((y+h)/2)),(230,80,50),3)
        cv2.putText(frame, "["+ str(x)+","+str(y)+"]", ((x,y)[0]+10,(x,y)[1]+10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
        cv2.imshow('camara',frame)#mostrar en pantalla la camara
#control de los motores
        if (x!=0) & (x>290)):
            m=(50+((x-300)*.16))         
            PWM_A.ChangeDutyCycle(m)#izquierda
            n=(50-(x*.16)) 
            PWM_B.ChangeDutyCycle(n)#derecha
        if x<290:
            m=(50-((x-300)*.16)) 
            PWM_A.ChangeDutyCy(x>=291) & (x<309)):cle(m)#izquierda
            n=(50+(x*.16)) 
            PWM_B.ChangeDutyCycle(n)#derecha
        if ((x>=291) & (x<309)):#revisar la logica
            PWM_A.ChangeDutyCycle(50)
            PWM_B.ChangeDutyCycle(50)
        if ((x==0) & (y==0)):
            camara=0
    PWM_A.ChangeDutyCycle(100)
    PWM_B.ChangeDutyCycle(100)
        
def startpwm():
    PWM_A.start(50)
    PWM_B.start(50)
    #camara=1
    #while (camara==1): #vaariable  detecta si hay recorrido o no (probar si es con 0 o con 1 el ciclo)
    Camara()
        
    
def stop_pwm():
    PWM_A.ChangeDutyCycle(100)
    PWM_B.ChangeDutyCycle(100)

#--------Main-------------------

print("go")
GPIO.add_event_detect(Start,GPIO.RISING,callback= startpwm,bouncetime=500)
while (1):
    if GPIO.input(Start)==0:
        print("start")
        travajo=1
        startpwm()

il_diavolo
Posts: 137
Joined: Mon Dec 02, 2013 7:56 pm

Re: cv2 camara problem (no img)

Wed Apr 17, 2019 9:34 pm

Pi camera or USB camera?

I have found that "cap=cv2.VideoCapture(0)" no longer works on my Pi's using the official Pi camera (version 1.0) since a recent update to Stretch, despite having worked for a couple of years previously. I have had to recode my programs to use Picamera instead. "cap=cv2.VideoCapture(0)" still works for my USB camera, however.

Crozz Bite
Posts: 14
Joined: Thu Jan 24, 2019 5:23 pm

Re: cv2 camara problem (no img)

Mon Apr 22, 2019 6:53 pm

il_diavolo wrote:
Wed Apr 17, 2019 9:34 pm
Pi camera or USB camera?

I have found that "cap=cv2.VideoCapture(0)" no longer works on my Pi's using the official Pi camera (version 1.0) since a recent update to Stretch, despite having worked for a couple of years previously. I have had to recode my programs to use Picamera instead.

still works for my USB camera, however.
yes is a usb camera , but i solve it XP
i put a

Code: Select all

cv2.waitKey(30)
and it works, it seems to me that i wasnt gave to the raspberry enough time to process the img and that was all the problem.

Return to “Python”