<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Import Video via QThread]]></title><description><![CDATA[<p dir="auto">Who can help with this code?<br />
How to rebuild it correctly?<br />
Initially, I only made an object detector, then I decided to use <code>QTread</code> to display the recognized video in the application.<br />
All recognition functionality is in <code>def onVideo</code>  how I can pass that part of code to <code>Worker1(QThread)</code> and connect it correctly</p>
<p dir="auto">I don't know how to pass the frame from the recognized video to QTread so that it is displayed.<br />
And for some reason, a video that is 25 seconds goes somewhere in 5 seconds.</p>
<p dir="auto">And how in this situation, when I import another video, so QTread terminates the old process and starts a new one.</p>
<p dir="auto">Where is some <code>###</code> I tried to do that without QTread but also don't understand how to terminate process with displaying detected video</p>
<pre><code>np.random.seed(20)
class Video_Detector_obj: 
    def __init__(self, videoPath, configPath, modelPath, classesPath):
        self.videoPath = videoPath
        self.configPath = configPath
        self.modelPath = modelPath
        self.classesPath = classesPath

        self.net = cv2.dnn_DetectionModel(self.modelPath, self.configPath)
        self.net.setInputSize(320, 320)
        self.net.setInputScale(1.0/127.5)
        self.net.setInputMean((127.5, 127.5, 127.5))
        self.net.setInputSwapRB(True)

        self.readClasses()
        
        self.load_video = QLabel()
        self.load_video.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.Worker1 = Worker1()
        self.Worker1.ImageUpdate.connect(self.ImageUpdatesSlot)

    def readClasses(self):
        with open(self.classesPath, 'r') as f:
            self.classesList = f.read().splitlines()
        
        self.classesList.insert(0, '__Background__')

        self.colorList = np.random.uniform(low=0, high=255, size=(len(self.classesList), 3))
        
        print(self.classesList)

    def ImageUpdatesSlot(self, Image):
        self.load_video.setPixmap(QPixmap.fromImage(Image))


    def CancelFeed(self):
        self.Worker1.stop()
    
    def onVideo(self, grid_video_detect):
        global image
        self.key = True
        cap = cv2.VideoCapture(self.videoPath)
        temp_path = self.videoPath

        if (cap.isOpened()==False):
            print("Error opening file...")
            return

        (success, image) = cap.read()

        startTime = 0

        while success:
            currentTime = time.time()
            fps = 1/(currentTime - startTime)
            startTime = currentTime
            classLabelIDs, confidences, bboxs =  self.net.detect(image, confThreshold = 0.5)

            bboxs = list(bboxs)
            confidences = list(np.array(confidences).reshape(1,-1)[0])
            confidences = list(map(float, confidences))

            bboxIdx = cv2.dnn.NMSBoxes(bboxs, confidences, score_threshold = 0.5, nms_threshold = 0.2)

            if len(bboxIdx) != 0:
                    for i in range(0, len(bboxIdx)):

                        bbox = bboxs[np.squeeze(bboxIdx[i])]
                        classConfidence = confidences[np.squeeze(bboxIdx[i])]
                        classLabelID = np.squeeze(classLabelIDs[np.squeeze(bboxIdx[i])])
                        classLabel = self.classesList[classLabelID]
                        classColor = [int(c) for c in self.colorList[classLabelID&rsqb;&rsqb;

                        displayText = "{}:{:.2f}".format(classLabel, classConfidence)

                        x,y,w,h = bbox

                        cv2.rectangle(image, (x,y), (x+w, y+h), color=classColor, thickness=1)
                        cv2.putText(image, displayText, (x, y-10), cv2.FONT_HERSHEY_PLAIN, 1, classColor, 2)

            cv2.putText(image, "FPS: " + str(int(fps)), (20, 70), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
            #cv2.imshow("Result", image)


            
            #load_video = QLabel()
            #image = imutils.resize(image, width = 1500)
            #frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            #image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
            #load_video.setPixmap(QPixmap.fromImage(image))
            #load_video.setAlignment(Qt.AlignmentFlag.AlignCenter)
            #grid_video_detect.addWidget(load_video, 0, 1)



            self.Worker1.start()
            grid_video_detect.addWidget(self.load_video, 0, 1)

            
            key = cv2.waitKey(1) &amp; 0xFF
            #if key == ord("q"):
            #    break

            (success, image) = cap.read()
        cv2.destroyAllWindows()


class Worker1(QThread):
    ImageUpdate = pyqtSignal(QImage)
    def run(self):
        self.TreadActive = True
        Capture = cv2.VideoCapture("Road - 84970.mp4")
        while self.TreadActive:
            ret, frame = Capture.read()
            if ret:
                Image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                #FlippedImage = cv2.flip(Image, 1)
                ConvertToQtFormat = QImage(Image.data, Image.shape[1], Image.shape[0], QImage.Format_RGB888)
                Pic = ConvertToQtFormat.scaled(1500, 3000, Qt.KeepAspectRatio)
                self.ImageUpdate.emit(Pic)
    def stop(self):
        self.TreadActive = False
        self.quit()

</code></pre>
]]></description><link>https://forum.qt.io/topic/142232/import-video-via-qthread</link><generator>RSS for Node</generator><lastBuildDate>Sat, 27 Jun 2026 17:02:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/142232.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Jan 2023 18:05:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Import Video via QThread on Mon, 16 Jan 2023 19:58:16 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Don't access GUI elements in threads other than the main thread. Use the Mandelbrot Example to see how you can process images in a secondary thread and send the result to the main thread for display purpose.</p>
<p dir="auto">As for the speed of the video reading, that's up to you to read the file at the pace you want/need. To the best of my memory, OpenCV is not a media player and does not provide support for playback based on the file metadata.</p>
]]></description><link>https://forum.qt.io/post/744029</link><guid isPermaLink="true">https://forum.qt.io/post/744029</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 16 Jan 2023 19:58:16 GMT</pubDate></item></channel></rss>