How to process images of a video, frame by frame, in video streaming using OpenCV and Python
By : betul
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further After reading the documentation of VideoCapture. I figured out that you can tell VideoCapture, which frame to process next time we call VideoCapture.read() (or VideoCapture.grab()). The problem is that when you want to read() a frame which is not ready, the VideoCapture object stuck on that frame and never proceed. So you have to force it to start again from the previous frame. code :
import cv2
cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("./out.mp4")
cv2.waitKey(1000)
print "Wait for the header"
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
# The frame is ready and already captured
cv2.imshow('video', frame)
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
print str(pos_frame)+" frames"
else:
# The next frame is not ready, so we try to read it again
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
print "frame is not ready"
# It is better to wait for a while for the next frame to be ready
cv2.waitKey(1000)
if cv2.waitKey(10) == 27:
break
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
# If the number of captured frames is equal to the total number of frames,
# we stop
break
|
How to get video frame by frame from stream using openCV and python
By : Dennis Preston
Date : March 29 2020, 07:55 AM
|
How to crop a webcam feed frame using OpenCV and C++
By : Rach
Date : March 29 2020, 07:55 AM
will be helpful for those in need I don't like C interface of Open CV, so I'd suggest you a possible solution via C++ interface of Open CV 3.4.0 code :
#include <Windows.h>
#include <Vfw.h>
#include <string>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\imgcodecs\imgcodecs.hpp"
#include "opencv2\highgui\highgui.hpp"
using namespace cv;
using namespace std;
int main()
{
VideoCapture camera;
camera.open(0);
if (!camera.isOpened()) {
cout << "Failed to open camera." << std::endl;
return -1;
}
Mat frame, oMatCrop;
while(1){ //Create loop for live streaming
camera >> frame;
oMatCrop=frame(cv::Rect(0,0,320,240));
cv::namedWindow("Image",CV_WINDOW_FREERATIO);
cv::imshow("Image", frame);
cv::namedWindow("Cropped image",CV_WINDOW_FREERATIO);
cv::imshow("Cropped image", oMatCrop);
if (waitKey(50) == 27) {
break;
}
}
return 0;
}
|
OpenCV: Code to Reconnect a Disconnected Camera Feed is Working Fine but in the Front End Captured Video Frame is Not ge
By : Drox
Date : March 29 2020, 07:55 AM
Any of those help Video feed is not getting stopped(more specifically in my case: that blank window is not going off) upon pressing 'q' from keyboard, although there is code written for this code :
def work_with_captured_video():
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
break
else:
cv2.imshow('frame', frame)
return True # Here You are returning the status.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def work_with_captured_video():
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
#break --> Not required.
else:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return True
while True:
camera = cv2.VideoCapture('rtsp://<ip specific to my camera>')
if camera.isOpened():
print('Camera is connected')
#call function
response = work_with_captured_video()
if response == False:
time.sleep(10)
continue
else:
print('Camera not connected')
camera.release()
time.sleep(10)
continue
def work_with_captured_video(camera):
while True:
ret, frame = camera.read()
if not ret:
print("Camera is disconnected!")
camera.release()
return False
else:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return True
while True:
camera = cv2.VideoCapture('rtsp://<ip specific to my camera>')
if camera.isOpened():
print('Camera is connected')
#call function
response = work_with_captured_video(camera)
if response == False:
time.sleep(10)
continue
else:
print('Camera not connected')
camera.release()
time.sleep(10)
continue
|
How to draw a line on frame from live video feed in opencv c++
By : user2863464
Date : March 29 2020, 07:55 AM
it fixes the issue As @Sunreef suggested, you can create separate cv::Mat to keep picture with lines only and display src combined with this picture code :
// #0 NEW - add declaration of lines here so this Mat is visible in both onMouse and main scope
cv::Mat src, lines;
void onMouse(int event, int x, int y, int f, void*)
{
if (f == 3)
{
cout << "start x,y is : " << start_x << start_y << endl;
int end_x = x;
int end_y = y;
cout << "end x,y is : " << end_x << end_y << endl;
// #1 NEW - draw line into lines instead of src
cv::line(lines, cv::Point(start_x, start_y), cv::Point(end_x, end_y), Scalar(255), 2, 8, 0);
// #2 NEW - remove unnecessary imshow here
run_once = false;
}
}
int main(int, char**)
{
for (;;)
{
cap >> src;
// #3 NEW - init lines once, to be the same size, same type as src filled with zeros
if(lines.empty()) lines = cv::Mat::zeros(src.size(), src.type());
// #4 NEW - show lines combined with lines
imshow(winName, lines + src);
if (waitKey(30) >= 0) break;
}
return 0;
}
|