Ask Your Question
-1

how to save video from YOLO in opencv

asked 2020-01-05 02:06:45 -0500

Fahad Ahmed gravatar image

i am trying to save the video with detected objects,it saves the video but dont show any detected object. CODE is import cv2 from darkflow.net.build import TFNet import numpy as np import time import os from PyQt5 import QtCore, QtWidgets, QtGui, uic from PyQt5.QtWidgets import QPushButton, QInputDialog, QLineEdit from PyQt5.uic import loadUi import sys from PIL import Image

option= { 'model':'cfg/yolo.cfg', 'load':'bin/yolov2.weights', 'threshold': 0.3, } class MyWindow(QtWidgets.QMainWindow):

def __init__(self):
    super(MyWindow, self).__init__()
    loadUi("new.ui", self)
    self.testcam.clicked.connect(self.CameraTest)
    self.start.clicked.connect(self.Camera)


def CameraTest(self):
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    while(True):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('CamTest', frame)

        k = cv2.waitKey(30) & 0xff
        if k == 27: # press 'ESC' to quit
            break
    cap.release()
    cv2.destroyAllWindows()
def Camera(self):

    tfnet = TFNet(option)
    colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

    cap= cv2.VideoCapture(0)
    #cap.set(cv2.CAP_PROP_FRAME_WIDTH, 512)
    #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 512)
    fourcc=cv2.VideoWriter_fourcc(*'XVID')
    out=cv2.VideoWriter('new.avi',fourcc,20.0,(640,480))

    while True:
        stime = time.time()
        ret, frame = cap.read()
        out.write(frame)
        if (ret==True):

            results = tfnet.return_predict(frame)
            for color, result in zip(colors, results):
                tl = (result['topleft']['x'], result['topleft']['y'])
                br = (result['bottomright']['x'], result['bottomright']['y'])
                label = result['label']
                confidence = result['confidence']
                text = '{}: {:.0f}%'.format(label, confidence * 100)
                frame = cv2.rectangle(frame, tl, br, color, 5)
                frame = cv2.putText(
                    frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
            cv2.imshow('frame', frame)
            print('FPS {:.1f}'.format(1 / (time.time() - stime)))
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break

    cap.release()
    out.release()
    cv2.destroyAllWindows()

if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-01-05 03:01:58 -0500

mvuori gravatar image

You write the frames to out before doing anything to them.

edit flag offensive delete link more

Comments

how can you show ? thanks

Fahad Ahmed gravatar imageFahad Ahmed ( 2020-01-05 03:07:59 -0500 )edit

okay got it thanks man

Fahad Ahmed gravatar imageFahad Ahmed ( 2020-01-05 03:14:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-01-05 02:06:45 -0500

Seen: 79 times

Last updated: Jan 05