D
D
Devil_Evil20212021-06-03 12:17:33
Python
Devil_Evil2021, 2021-06-03 12:17:33

How to describe how to handle an exception when an attempt to connect to an IP camera fails?

1. How to describe the exception handling when an unsuccessful attempt to connect to the broadcast of an IP camera in the local network?
If you just use try/except, then nothing happens.
There is only an error in the console, but the except block does not reach, but the program does not crash.
60b89cb4d59a2377231276.png
60b89cdcda263002070147.png

a1 = self.lineIPadress.text()
        if a1 == '':
            exPR.about(self, "Ошибка", "Поле IP-адреса не должно быть пустым.")
        else:

            #cam = cv2.VideoCapture('http://' + a1 + '/video')
            #cam = cv2.VideoCapture(a1)
            try:
                w1 = cam.get(cv2.CAP_PROP_FRAME_WIDTH)
                h1 = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)

                minW = 0.1*w1
                minH = 0.1*h1

                names = facename

                while(cam.isOpened()):
        
                    ret, img =cam.read()

                    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                    faces = faceCascade.detectMultiScale( 
                         gray,
                         scaleFactor = 1.2,
                         minNeighbors = 5,
                         minSize = (int(minW), int(minH)),
                         )

                     for(x,y,w,h) in faces:

                        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)

                        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])

                        if (confidence < 100):
                            id = names[id]
                            confidence = "  {0}%".format(round(100 - confidence))
               
                        else:
                            id = "unknown"
                            confidence = "  {0}%".format(round(100 - confidence))

                        cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
                        cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1) 
                    
                    cv2.imshow('Camera',img)             
       
                    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
                    if k == 27:
                        break

                cam.release()
                cv2.destroyAllWindows()
            except:
                print("ошибка")


2. What is better to write at the end of the IP address to connect to any IP camera on the local network if it does not have a password / login?
For example, 192.168.43.1:8080/video (in this case, it works if the phone is used as an IP camera) or 192.168.43.1:8080/1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-06-03
@Devil_Evil2021

1. OpenCV can work with four video sources: a video file, a set of frame files, a connected camera, a video stream over the network. In the second screenshot, the error about CAP_IMAGES hints that OpenCV is trying to interpret the URL in the second mode instead of the fourth. It is worth specifying an additional parameter of the VideoCapture constructor, which sets the mode for opening the video source.
2. The OpenCV API is rather inconsistent in its error signaling, and only throws exceptions if invalid data is fed to the function. Situations such as incorrect aspect ratio are signaled by the return values. In your case, all that remains is to wait a couple of seconds, periodically polling isOpened(). If it does not return true at the end of the interval, we consider the attempt failed. The alternative is to try and capture the messages to the console, which seems to be output by the standard logging module. But this is another hack.
3. The exact URL depends on the type and configuration of the software used for web streaming. You don't know in advance. At most, you can try to google the default URLs for the most popular versions of such software and try them one by one.

I
Ilya Efimov, 2021-06-03
@A_M

Not competent enough to answer 1 part of the question.
There is a commentary on the second part. As I understand it, you are currently working with mjpeg over http (judging by the format of the request). In modern cameras, working with mjpeg is a value tending to zero. Most cameras work through rtsp streams, which can be parsed into frames in ffmpeg and only then fed to opencv (but this is already beyond my competence, theoretical reflections) Regarding the issue of choosing the format of an rtsp request (or mjpeg request) - or individually integrate the requests of each manufacturer, or deal with getting it from the ONVIF profile (you should be interested in profile S and T)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question