opencv
推荐博客:https://blog.csdn.net/wwxy1995/article/details/79735283
import cv2 def make_photo(): """使用opencv拍照""" cap = cv2.VideoCapture(0) # 默认的摄像头 while True: ret, frame = cap.read() if ret: cv2.imshow("capture", frame) # 弹窗口 # 等待按键q操作关闭摄像头 if cv2.waitKey(1) & 0xFF == ord('q'): file_name = "xieyang.jpeg" cv2.imwrite(file_name, frame) break else: break cap.release() cv2.destroyAllWindows() def make_video(): """使用opencv录像""" cap = cv2.VideoCapture(0) # 默认的摄像头 # 指定视频代码 fourcc = cv2.VideoWriter_fourcc(*"DIVX") out = cv2.VideoWriter('xieyang.avi', fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret: out.write(frame) # cv2.imshow('frame',frame) # 等待按键q操作关闭摄像头 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() if __name__ == '__main__': make_video()
客户端
import cv2 import numpy import matplotlib.pyplot as plot import requests # 测试摄像头 cap = cv2.VideoCapture(0) while(1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() # 拍摄图片保存 import cv2 cap = cv2.VideoCapture(0) while(1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("test.jpeg", frame) break cap.release() cv2.destroyAllWindows() # 录制视频保存 cap = cv2.VideoCapture(0) # 默认的摄像头 # 指定视频代码 fourcc = cv2.VideoWriter_fourcc(*"DIVX") # print(fourcc) out = cv2.VideoWriter('xieyang.avi', fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret: out.write(frame) print(len(frame)) cv2.imshow('frame',frame) # 等待按键q操作关闭摄像头 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() # 上传图片 files = {'file': open('fangjian2.jpeg', 'rb')} response =requests.post('http://127.0.0.1:8000/test/', files=files) # response =requests.post('http://127.0.0.1:8000/test/', data={"data":'sddggggh'}) form表单提交 ,也可以上传json print(response.text) #上传视频 files = {'file': open('xieyang.avi', 'rb')} response =requests.post('http://127.0.0.1:8000/test/', files=files) print(response.text) # 同步录制上传 cap = cv2.VideoCapture(0) # 默认的摄像头 while (1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # 同步上传是有问题的,现在先不考虑这个了 requests.post('http://127.0.0.1:8000/test/', data=frame.tobytes(),headers={"Content-Type":"multipart/form-data"}) cap.release() cv2.destroyAllWindows()
服务端
from django.shortcuts import render,HttpResponse,redirect def test(request): # 接收上传的视频,这个是可以的 file_obj = request.FILES.get('file') print(file_obj,type(file_obj)) f = open( file_obj.name, 'wb') for chunk in file_obj.chunks(): f.write(chunk) f.close() """ # 不知道是不是post请求改变了数据的编码,在request.body中接收的bytes和发送的bytes并不一样,导致写入的文件不能读取 file_obj=request.body print(file_obj,type(file_obj)) print(22222) # 这可以说明就只有一次连接 f = open('test.jpeg', 'wb') f.write(file_obj) # for chunk in file_obj.chunks(): # f.write(chunk) """ return HttpResponse(111)
使用js调用摄像头:https://www.cnblogs.com/weixing/p/6555681.html