86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
from app.model.bussiness_model import ProjectImage, ProjectInfo, ProjectImgLeafer, ProjectImgLabel
|
|
from app.model.schemas.project_info_schemas import ProjectInfoIn, ProjectInfoOut
|
|
from app.model.schemas.project_image_schemas import ProjectImgLeaferLabel, ProjectImgLeaferOut
|
|
from app.model.crud import project_info_crud as pic
|
|
from app.model.crud import project_image_crud as pimc
|
|
from app.model.crud import project_img_leafer_label_crud as pillc
|
|
from app.util import os_utils as os
|
|
from app.util import random_utils as ru
|
|
from app.config.config_reader import datasets_url, runs_url, images_url
|
|
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from fastapi import UploadFile
|
|
|
|
|
|
def add_project(info: ProjectInfoIn, session: Session, user_id: int):
|
|
"""
|
|
新建项目,完善数据,并创建对应的文件夹
|
|
:param info: 项目信息
|
|
:param session: 数据库session
|
|
:param user_id: 用户id
|
|
:return:
|
|
"""
|
|
project_info = ProjectInfo(**info.dict())
|
|
project_info.user_id = user_id
|
|
project_info.project_no = ru.random_str(6)
|
|
project_info.project_status = "0"
|
|
project_info.train_version = 0
|
|
os.create_folder(datasets_url, project_info.project_no)
|
|
os.create_folder(runs_url, project_info.project_no)
|
|
pic.add_project(project_info, session)
|
|
return project_info
|
|
|
|
|
|
def upload_project_image(project_info: ProjectInfoOut, files: List[UploadFile], session: Session):
|
|
"""
|
|
上传项目的图片
|
|
:param files: 上传的图片
|
|
:param project_info: 项目信息
|
|
:param session:
|
|
:return:
|
|
"""
|
|
images = []
|
|
for file in files:
|
|
image = ProjectImage()
|
|
image.project_id = project_info.id
|
|
# 保存原图
|
|
path = os.save_images(images_url, project_info.project_no, file=file)
|
|
image.image_url = path
|
|
# 生成缩略图
|
|
thumb_image_url = images_url + "\\thumb\\" + project_info.project_no + "\\" + ru.random_str(10) + ".jpg"
|
|
os.create_thumbnail(path, thumb_image_url)
|
|
image.thumb_image_url = thumb_image_url
|
|
images.append(image)
|
|
pimc.add_image_batch(images, session)
|
|
|
|
|
|
def save_img_label(img_leafer_label: ProjectImgLeaferLabel, session: Session):
|
|
"""
|
|
保存图片的标签框选信息,每次保存都会针对图片的信息全部删除,然后重新保存
|
|
:param img_leafer_label:
|
|
:param session:
|
|
:return:
|
|
"""
|
|
img_leafer = ProjectImgLeafer(**img_leafer_label)
|
|
pillc.save_img_leafer(img_leafer, session)
|
|
label_infos = img_leafer_label.label_infos
|
|
img_labels = []
|
|
for label_info in label_infos:
|
|
img_label = ProjectImgLabel(**label_info.dict())
|
|
img_label.image_id = img_leafer_label.image_id
|
|
img_labels.append(img_label)
|
|
pillc.save_img_label_batch(img_leafer_label.image_id, img_labels, session)
|
|
|
|
|
|
def get_img_leafer(image_id: int, session: Session):
|
|
"""
|
|
根据图片id查询图片的leafer信息
|
|
:param image_id:
|
|
:param session:
|
|
:return:
|
|
"""
|
|
img_leafer = pillc.get_img_leafer(image_id, session)
|
|
img_leafer_out = ProjectImgLeaferOut.from_orm(img_leafer).dict()
|
|
return img_leafer_out
|