Doubt Python Code and Opencv2

Asked

Viewed 80 times

1

In this code I can do facial detection from a webcan. I use my notebook camera. I want this code to send a text message, for example "Detected Person" every time it detects a face. I tried to put an "if" but got in the way.

(if len(facesDetectadas)>=1:
        print ("PESSOA DETECTADA")

Another question I have is that in this code I am accessing the camera of my notebook (video = cv2.VideoCapture(0)). If I want to access a q camera is connected to a server, as would this line of code?

Below follows my code. Thank you for your attention.

import cv2

video = cv2.VideoCapture(0)
classificadorFace = cv2.CascadeClassifier('cascades\\haarcascade_frontalface_default.xml')

while True:
    conectado, frame = video.read()

    frameCinza = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    facesDetectadas = classificadorFace.detectMultiScale(frameCinza, minSize=(70,70))
    for (x, y, l, a) in facesDetectadas:
        cv2.rectangle(frame, (x, y), (x + l, y + a), (0, 0, 255), 2)

    cv2.imshow('Vídeo', frame)

    if cv2.waitKey(1) == ord('a'):
        break

    if len(facesDetectadas)>=1:
        print ("PESSOA DETECTADA")

video.release()
cv2.destroyAllWindows()
  • Hello @Rodrigo, I see that you have been a member of the site for some time, but it seems to me that you are still not very set on how the site works so it is worth you to take a look at our [Tour]I say this because I see that you have asked a total of nine questions so far and even though the answers given to you have helped you have never accepted an answer, although it seems that it has solved your problem. This is not good for the health of the site, because your question gets like unresolved, Accepting an answer will reward those who have helped you and help those who have a problem. = D

  • 1

    Thanks for the tip. I looked at the tour of the site and found great. There was no attack me for this detail and I corrected it. Thanks for the attention.

1 answer

0


I want this code to send a text message, for example "Detected Person" every time it detects a face.

You’re doing it right. By doing the check if len(facesDetectadas)>=1, you are checking if there was at least one face detected. I tested your code and everything worked as expected.

Make sure you are not having any errors with your code. For example, if the file haarcascade_frontalface_default.xml is inside a folder called cascades in the same directory as your file .py.

Also make sure you are accessing the webcam correctly. With this code of yours, you’re expected to open a window streaming your webcam, and when you recognize a face, it should have a red square around it.

Its clause if is correct. If you are still in error, it is with another part of executing your code.

If I want to access a q camera is connected to a server, as would this line of code?

You will have to access the camera via its network address and port.

video = cv2.VideoCapture('endereço_ip:porta')

See the exact address of the streaming camera you want to work with. You may still be inside a javascript function in directory at the specified address, for example:

video = cv2.VideoCapture('endereço_ip:porta/?action=stream')

Or in an MJPEG:

video = cv2.VideoCapture('endereço_ip:porta/nome_do_arquivo.mjpeg')

There are several other ways, but everything will depend on your camera installation setup.

  • Thanks for the tip.

  • If you are still in error after checking what I said, show us here to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.