class PorderReportAiPicture(GenericAPIView):
@swagger_auto_schema(operation_summary="生产报工-AI拍照", tags=['生产管理/生产报工'])
def post(self, request):
porder_id = request.data['porder_id']
station_id = request.data['station_id']
machine_sn = request.data['machine_sn']
stage_id = request.data['stage_name']
porder = PorderInfo.objects.filter(id=porder_id,is_delete=0).first()
ai = RealityProduceStation.objects.filter(stage_name=1,porder_id=porder_id,id=station_id,is_delete=0).first()
ip = Camera.objects.filter(company_id=porder.company_id,shop_id=porder.car_id,line_id=porder.line_id,sequence=ai.station_sequence,is_delete=0).first().camera_ip
camera = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % ("admin", "100trust", ip, 1))
file_path = os.path.join(settings.BASE_DIR, 'media')
# 文件名
object_name = machine_sn + '_' + str(time.time()) + ".jpg"
if sys.platform.startswith('win'):
new_path = file_path + '\\' + object_name
else:
new_path = file_path + '/' + object_name
# 保存本地
cv2.imwrite(new_path, camera.read()[1])
# 关闭相机
camera.release()
# 获取文件类型
file_type, _ = mimetypes.guess_type(new_path)
# 获取文件大小
content = os.stat(new_path)
# 将刚保存的图片上传minio
with open(new_path, 'rb') as file:
pic_url = MinioClass.MinioClass.upload_local_image(object_name, file, content.st_size, file_type)
# 将数据上传到数据库
CameraPicture.objects.create(
porder_id = porder_id,
stage_id = stage_id,
station_id = station_id,
pic_url = pic_url,
machine_sn = machine_sn,
created_by = request.user,
created_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
)
# 删除本地
os.remove(new_path)
# 获取所有拍摄照片
pic_info = CameraPicture.objects.filter(porder_id=porder_id,stage_id=stage_id,station_id=station_id,machine_sn=machine_sn,is_delete=0).order_by('-id').first()
pic_data = []
pic_data.append(MinioClass.MinioClass.preview_image(pic_info.pic_url))
return R.success(pic_data)
Django 摄像头
于 2024-01-17 10:45:55 首次发布
该篇文章详细描述了一个API视图类,用于生产报工场景中的AI拍照功能。它通过获取请求参数,连接摄像头,捕获图片,保存到本地并上传到Minio存储,最后将数据存入数据库并返回预览图片。
摘要由CSDN通过智能技术生成