我正在尝试保存视频,但它无法正常工作.我按照openCV文档中的说明进行操作.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
怎么了?
我必须拼接从许多 (9) 台相机捕获的图像。最初,我尝试从 2 个相机以 15 FPS 的速率捕获帧。然后,我连接了 4 个摄像头(我还使用了外部供电的 USB 集线器来提供足够的电力)但我只能看到一个流。
为了测试,我使用了以下脚本:
import numpy as np
import cv2
import imutils
index = 0
arr = []
while True:
cap = cv2.VideoCapture(index)
if not cap.read()[0]:
break
else:
arr.append(index)
cap.release()
index += 1
video_captures = [cv2.VideoCapture(idx) for idx in arr]
while True:
# Capture frame-by-frame
frames = []
frames_preview = []
for i in arr:
# skip webcam capture
if i == 1: continue
ret, frame = video_captures[i].read()
if ret:
frames.append(frame) …Run Code Online (Sandbox Code Playgroud) 我在 RasbpberryPI 3 上运行了简单的 python 脚本。这个脚本负责使用 MJPEG 打开视频设备和流数据 (800x600) 到 HTTP 端点。当我收到这个流时,我的一个 rasbpberrypi 核心 100% 工作。可以使用多线程运行 OpenCV 吗?
这是我的代码
import cv2
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import time
import argparse
import socket as Socket
camera = None
def setUpCameraCV():
global camera
camera = cv2.VideoCapture(0)
class mjpgServer(BaseHTTPRequestHandler):
ip = None
hostname = None
def do_GET(self):
print('connection from:', self.address_string())
if self.ip is None or self.hostname is None:
self.ip, _ = 0.0.0.0
self.hostname = Socket.gethostname()
if self.path == '/mjpg':
self.send_response(200)
self.send_header('Cache-Control', 'no-cache')
self.send_header('Pragma', …Run Code Online (Sandbox Code Playgroud) 我正在使用OpenCV开发Python模块,该模块连接到RTSP流以对视频执行一些预处理(主要是降低fps和分辨率),然后将其存储在文件系统中。
但是,即使尝试了几种编解码器,也正在寻找类似的发展……我总是以空虚的视频告终。我看过其他线程(cv :: VideoWriter产生不可读的视频),这可能很相似,但是是在C ++上开发的。
有人为此工作吗?我通常使用示例RTSP流作为参考,例如rtsp://freja.hiof.no:1935 / rtplive / definst /hessdalen03.stream,并且可以正确接收甚至观看来自VLC的流。
我已经看到很多线程讨论如何从RTSP流中捕获视频,或者如何与VideoWriters和VideoReaders类以及视频文件一起工作,但是几乎没有结合这两者的内容。
任何帮助将不胜感激:)谢谢!
编辑1:用于存储框架的示例代码。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy
# Test frame.
width, height = 400, 300
width_2, height_2 = int(width / 2), int(height / 2)
frame = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame, (0, 0), (width_2, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame, (width_2, height_2), (width, height), (0, 255, 0), cv2.FILLED)
frames = [frame for _ in range(100)]
fps = 25
# Define …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,在遇到栏杆的第一个像素的视频上画一条线,我的问题是视频播放缓慢。
屏幕截图,供您参考视频的外观。在录像过程中,摄像机移近了,但由于速度慢,我不得不等待几分钟才能看到变化,但是在拍摄时,摄像机每隔几秒钟就移动一次。
我认为问题是for循环在视频的每一帧上都起作用,但我不确定。
我可以采用什么解决方案来加快程序执行速度?
import cv2
cap = cv2.VideoCapture('video.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
canny = cv2.Canny(frame, 85, 255)
height, width = canny.shape
first_black_array = []
for x in range(width):
first_black_pixel_found = 0
for y in range(height):
if first_black_pixel_found == 0:
if canny[y,x] == 255:
first_black_array.append(height - y)
first_black_pixel_found = 1
cv2.line(frame,(x,y),(x,y),(0,255,0),1)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试从 IP 摄像头获取 python 中的视频流,但出现错误。我正在使用 Pycharm IDE。
import cv2
scheme = '192.168.100.23'
host = scheme
cap = cv2.VideoCapture('http://admin:Ebmacs8485867@'+host+':81/web/admin.html')
while True:
ret, frame = cap.read()
# Place options to overlay on the video here.
# I'll go over that later.
cv2.imshow('Camera', frame)
k = cv2.waitKey(0) & 0xFF
if k == 27: # esc key ends process
cap.release()
break
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
import cv2
scheme = '192.168.100.23'
host = scheme
cap = cv2.VideoCapture('http://admin:Ebmacs8485867@'+host+':81/web/admin.html')
while True:
ret, frame = cap.read()
# Place options to overlay …Run Code Online (Sandbox Code Playgroud)