Why does not my python code work?

-1
import numpy as np
import cv2
import time
# Cargamos el vídeo
camara = cv2.VideoCapture(0)
# Inicializamos el primer frame a vacío.
# Nos servirá para obtener el fondo
fondo = None
# Recorremos todos los frames
while True:
# Obtenemos el frame
    grabbed, frame = camara.read()
# Si hemos llegado al final del vídeo salimos
if not grabbed:
    break
# Convertimos a escala de grises
gris = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Aplicamos suavizado para eliminar ruido
gris = cv2.GaussianBlur(gris, (21, 21), 0)
# Si todavía no hemos obtenido el fondo, lo obtenemos
# Será el primer frame que obtengamos
if fondo is None:
    fondo = gris
continue
# Calculo de la diferencia entre el fondo y el frame actual
resta = cv2.absdiff(fondo, gris)
# Aplicamos un umbral
umbral = cv2.threshold(resta, 25, 255, cv2.THRESH_BINARY)[1]
# Dilatamos el umbral para tapar agujeros
umbral = cv2.dilate(umbral, None, iterations=2)
# Copiamos el umbral para detectar los contornos
contornosimg = umbral.copy()
# Buscamos contorno en la imagen
im, contornos, hierarchy = cv2.findContours(contornosimg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Recorremos todos los contornos encontrados
for c in contornos:
# Eliminamos los contornos más pequeños
    if cv2.contourArea(c) < 500:
        continue
# Obtenemos el bounds del contorno, el rectángulo mayor que engloba al contorno
(x, y, w, h) = cv2.boundingRect(c)
# Dibujamos el rectángulo del bounds
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Mostramos las imágenes de la cámara, el umbral y la resta
cv2.imshow("Camara", frame)
cv2.imshow("Umbral", umbral)
cv2.imshow("Resta", resta)
cv2.imshow("Contorno", contornosimg)
# Capturamos una tecla para salir
key = cv2.waitKey(1) & 0xFF
# Tiempo de espera para que se vea bien
time.sleep(0.015)
# Si ha pulsado la letra s, salimos
if key == ord("s"):
    break
# Liberamos la cámara y cerramos todas las ventanas
camara.release()
cv2.destroyAllWindows()

When I run, it gives me the result:

runfile('C:/Users/Franz/Desktop/Sin título 0.py', wdir='C:/Users/Franz/Desktop')
  File "C:/Users/Franz/Desktop/Sin título 0.py", line 23
SyntaxError: 'break' outside loop

Please can someone help me, thanks in advance.

    
asked by Fanzo92 02.07.2017 в 02:16
source

1 answer

2

Code blocks are determined by indentation. The only delimiter is the two points and the indentation or indentation of the code itself, unlike other languages that use keys or reserved words ( begin , end , etc).

The error occurs because a break serves to break the cycle in which it is included. In your case, one of the problems is here:

while True:
# Obtenemos el frame
    grabbed, frame = camara.read()
# Si hemos llegado al final del vídeo salimos
if not grabbed:
    break

The conditional if is outside the while , it is a different block because it is at the same indentation level as the while . This implies that break is not within any cycle, hence the error.

In this case it should be:

while True:
    # Obtenemos el frame
    grabbed, frame = camara.read()
    # Si hemos llegado al final del vídeo salimos
    if not grabbed:
        break

Now the if is within the block of while and the break within the if , and therefore the while . When the condition break is met, it will break the one belonging to while .

In addition to the errors of indentation there is a conditional that does not make sense:

# Eliminamos los contornos más pequeños
    if cv2.contourArea(c) < 500:
        continue

As it is, without a else later, this does not do anything. If the contour is less than 500 continue with the next line, if it is not, also.

It is missing almost all the indentation, I do not know if by mistake when copying or because you have it, in any case I leave the code as I think it should be:

import numpy as np
import cv2
import time


# Cargamos el vídeo
camara = cv2.VideoCapture(0)
# Inicializamos el primer frame a vacío.
# Nos servirá para obtener el fondo
fondo = None
# Recorremos todos los frames
while True:
# Obtenemos el frame
    grabbed, frame = camara.read()
    # Si hemos llegado al final del vídeo salimos
    if not grabbed:
        break

    # Convertimos a escala de grises
    gris = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Aplicamos suavizado para eliminar ruido
    gris = cv2.GaussianBlur(gris, (21, 21), 0)
    # Si todavía no hemos obtenido el fondo, lo obtenemos
    # Será el primer frame que obtengamos
    if fondo is None:
        fondo = gris
        # Calculo de la diferencia entre el fondo y el frame actual
    resta = cv2.absdiff(fondo, gris)
    # Aplicamos un umbral
    umbral = cv2.threshold(resta, 25, 255, cv2.THRESH_BINARY)[1]
    # Dilatamos el umbral para tapar agujeros
    umbral = cv2.dilate(umbral, None, iterations=2)
    # Copiamos el umbral para detectar los contornos
    contornosimg = umbral.copy()
    # Buscamos contorno en la imagen
    contornos, hierarchy = cv2.findContours(contornosimg,cv2.RETR_TREE,cv2.CHAIN_APPROX‌​‌​_SIMPLE)
    # Para OpenCv3 la linea anterior debe ser:
    # im, contornos, hierarchy = cv2.findContours(contornosimg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # Recorremos todos los contornos encontrados
    for c in contornos:
    # Eliminamos los contornos más pequeños
        if cv2.contourArea(c) > 500:
            # Obtenemos el bounds del contorno, el rectángulo mayor que engloba al contorno
            (x, y, w, h) = cv2.boundingRect(c)
            # Dibujamos el rectángulo del bounds
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
           # Mostramos las imágenes de la cámara, el umbral y la resta
    cv2.imshow("Camara", frame)
    cv2.imshow("Umbral", umbral)
    cv2.imshow("Resta", resta)
    cv2.imshow("Contorno", contornosimg)
    # Capturamos una tecla para salir
    key = cv2.waitKey(1) & 0xFF
    # Tiempo de espera para que se vea bien
    time.sleep(0.015)
    # Si ha pulsado la letra s, salimos
    if key == ord("s"):
        break
# Liberamos la cámara y cerramos todas las ventanas
camara.release()
cv2.destroyAllWindows()
    
answered by 02.07.2017 в 02:58