Python实现mjpeg视频流1. 从摄像头获取图像,然后通过mjpeg stream⽅式显⽰。
# -*- coding: utf-8 -*-
#MJPEG Server for the webcam
import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import cv2.cv as cv
import re
import sys
import imutils
import socket
capture = cv.CaptureFromCAM(0)
img1 = cv.QueryFrame(capture)
if img1 == None :
print "No WebCam Found!"
if len(sys.argv) < 2 :
print "Usage : webcamserver <quality> <port>"
cameraQuality = 100
port = 8080
else:
cameraQuality = sys.argv[1]
port = int(sys.argv[2])
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
global cameraQuality
try:
self.path=re.sub('[^.a-zA-Z0-9]', "",str(self.path))
if self.path=="" or self.path==None or self.path[:1]==".":
return
if dswith(".html"):
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type',    'text/html')
self.wfile.ad())
f.close()
return
if dswith(".mjpeg"):
self.send_response(200)
self.wfile.write("Content-Type: multipart/x-mixed-replace; boundary=--aaboundary")
self.wfile.write("\r\n\r\n")
while 1:
img1 = cv.QueryFrame(capture)
cv2mat1 = cv.EncodeImage(".jpeg", img1, (cv.CV_IMWRITE_JPEG_QUALITY, cameraQuality))
JpegData1 = string()
self.wfile.write("--aaboundary\r\n")
self.wfile.write("Content-Type: image/jpeg\r\n")
self.wfile.write("Content-length: "+str(len(JpegData1))+"\r\n\r\n" )
学python看谁的视频比较好self.wfile.write(JpegData1)
self.wfile.write("\r\n\r\n\r\n")
time.sleep(0.02)
return
if dswith(".jpeg"):
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type','image/jpeg')
self.wfile.ad())

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。