Video
Provides interface between the Qml type SVideo and python. So to communicate with SVideo in python you will have to use Video.
import soloman 2.2
example.qml
import QtQuick 2.14
import QtQuick.Controls 2.14
import solomon 2.2
ApplicationWindow {
visible: true
width: 800
height: 500
SVideo {
id: scr_01
objectName: "scr_01" // declare objectName to be used in python
}
}
example.py
# -*- coding: utf-8 -*-
import sys
import cv2
import threading
from time import sleep
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml QQmlApplicationEngine
import soloman
app = QGuiApplication(sys.argv)
# Create a QML engine.
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit)
engine.load(QUrl('example.qml'))
# Get SVideo
vid = soloman.Video(engine)
vid.get_SVideo('scr_01') # objectName goes here
# Capture
capture = cv2.VideoCapture(0) # capture camera
def start_capt():
# start thread
o_thread = threading.Thread(target=_start_capt)
o_thread.daemon = True
o_thread.start()
def _start_capt():
while True:
ret, frame = capture.read()
if not ret:
break
vid.show_frame(frame)
sleep(1/24)
# Call to start capturing
start_capt()
# Run the app
ret_value = app.exec_()
capture.release()
sys.exit(0)
The above example uses the show_frame
to display the frame. This is best for showing a capturing device like a camera. But for a video as OpenCV is capable of, this can but isn't very suitable for the job. Use update
instead.
...
# Capture
capture = cv2.VideoCapture('mining.mp4')
...
def _start_capt():
while True:
...
vid.update(frame)
sleep(1/40) # use 40 just to be safe for 24 frames_per_second
...
It is also important you beef up your refresh rate for the update
. It plays 24 frames per second. In case some other thing is running so that, your program runs slow, the update
is not very patient, it is going to read that as an end of file. So a sleep function of exactly 0.042 seconds sleep(1/24)
, is fine, but unsafe, beef it up a little, like say 0.025 seconds sleep(1/40)
.
- _init_
- get_current_frame
- get_SVideo
- show_frame
- update
Does some initialization.
__init__(engine=None)
engine
is the QmlEngine
instance returned by calling either QmlEngine()
or QQmlApplicationEngine()
It is called automatically when you create an instance of the class
vid = soloman.Video(QmlEngine)
Gives you the current frame.
get_current_frame()
frame = solomon.Video(engine).get_SVideo('scr_01').get_current_frame()
frame
is the filename of a temporary file, copy it to a location you can access later.
Sets the corresponding handler for the SVideo Qml type so we can use to control it from python
get_SVideo(objectName)
objectName
is the objectName declared in Qml like:
Svideo {
objectName: "sv_01"
}
so it becomes:
vid = soloman.Video()
svid = vid.get_SVideo("sv_01")
show_frame
is like an instant update.
show_frame(frame)
frame
is a numpy array, which is a datatype cv2.VideoCapture
returns.
It is suitable for controlling your own frames per second. It is also less on system resources.
It is excellent for capturing devices.
cap = cv2.VideoCapture(0) # capture the camera
while True:
ret, frame = cap.read()
solomon.Video(engine).get_SVideo('scr_01').show_frame(frame)
sleep(1/24)
update
is best suited for video analysis
update(frame)
frame
is a numpy array, which is a datatype cv2.VideoCapture
returns.
It is suitable for video analysis. It plays 24fps even if you supply it with more frames per each second, meaning it stores it in a list and plays it later. For that reason, if you supply it with less than 24 frames per second it reads that as an end-of-file and exits. When reading from a video file with cv2, please make sure the seconds you pass into the sleep function is below 0.025 seconds, i.e. sleep(1/40)
cap = cv2.VideoCapture('whether.mp4') # capture from a video feed
while True:
ret, frame = cap.read()
solomon.Video(engine).get_SVideo('scr_01').update(frame)
sleep(1/40)
N/A