๐ ํ๋ก ํธ์๋ - Tracycle_WebProject_Front
๐ ๋ฐฑ์๋ - Tracycle_WebProject_Back
- ๊ธฐ๋ณธ ํ๊ฒฝ
- IDE : VS Code
- OS : Windows
- Git
- Jupyter Notebook
- ์น์๋น์ค ๊ฐ๋ฐํ๊ฒฝ
- Python
- Flask
- Pytorch
- ์ฌ์ฉ์๋ก๋ถํฐ ์ ๋ ฅ๊ฐ์ ๋ฐ๊ณ yolo5๋ก ์ด๋ฏธ์ง๋ฅผ ์ธ์ํ ํ ํด๋น ์ง์ญ๊ตฌ์ ์ด๋ฏธ์ง ๋ผ๋ฒจ์ ํด๋นํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด
userId = request.form.get('userId')
if request.method == 'POST':
areaId = int(request.form.get('areaId'))
if "mainFile" not in request.files:
return redirect(request.url)
file = request.files["mainFile"]
- userId, areaId, mainFile์ ์ ๋ ฅ๋ฐ๋๋ค
img_bytes = file.read()
img = Image.open(io.BytesIO(img_bytes))
results = model(img, size=640)
results.render()
for img in results.imgs:
img_base64 = Image.fromarray(img)
img_base64.save("static/"+userId+".jpg", format="JPEG")
data = results.pandas().xyxy[0].to_json(orient="records")
- image ๋ฐ์ ๊ฒ์ model()์ ๋ฃ์ด์ yolo๋ก ๋ถ์
- base64๋ก ๋ ๋๋งํด์ userId + jpg๋ก ์ ์ฅ
- ๋ถ์์์ ๋์จ label์ json์ผ๋ก ์ ์ฅ
info_list = list()
list_data = json.loads(data)
class_id=set()
for x in list_data:
class_id.add(x['class'])
class_id = list(class_id)
if not class_id:
print("Can't find object")
infos = "Can't find object"
else:
for c in class_id:
categoryId = c
infos = get_result('tracycle', areaId, categoryId)
for info in infos:
info_list.append(info)
return jsonify(info_list)
- ๋ถ์๊ฒฐ๊ณผ ๋ผ๋ฒจ๋ง๋ ์นดํ ๊ณ ๋ฆฌ๊ฐ ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ ํด๋น ๊ฒฐ๊ณผ๋ฅผ ์ ๋ถ ๋ฐ์ ๋ฆฌ์คํธ์ ์ ์ฅํ json์ผ๋ก ๋ฆฌํด
- ์์ ์ ํ๋ผ์คํฌ์ yolo ๊ตฌ๋
- ํ๋ผ์คํธ ์ค์ (port, host ์กฐ๊ฑด)
- yolo ์ค์ (torch๋ฅผ ํตํด yolov5 ์ ์ฒด ์ฝ๋ ๋ฐ์์ ์ ๋ ฅ, custom ํ์ผ๋ก ์ค์ ํ ptํ์ผ ์์น ์ค์ )
parser = argparse.ArgumentParser(
description="Flask app exposing yolov5 models")
parser.add_argument("--port", default=8085, type=int, help="port number")
args = parser.parse_args()
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt').autoshape() # force_reload = recache latest code
model.eval()
# debug=True causes Restarting with stat
app.run(host="0.0.0.0", port=args.port)
- Yolo v5 ๋ค์ด๋ก๋ ๋ฐ ์ค์น.
!git clone https://github.com/ultralytics/yolov5
!cd yolov5;pip install -qr requirements.txt
- ๋์ฉ๋ ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ Google Drive ์ ๊ทผ์ ์ํ ๋ง์ดํธ
import os, sys
from google.colab import drive
drive.mount('/content/gdrive')
- ์๋ฃ ์ ์ ํ์ฌ ๋ผ๋ฒจ๋ง, ๊ฒฐ๋ก ์ค์ ์์ ์ ์ ์ํ yaml ์์ฑ ํ์ผ ๋ฐ์์ค๊ธฐ
!wget -O /content/gdrive/MyDrive/Project_Data/Tracycle_Train.yaml https://raw.github.com/Koartifact/Tracycle_Ai/master/data/util/Tracycle_Train.yaml
- Train ์ํ ๋ถ๋ถ
- img 600 : ์ด๋ฏธ์ง ์ฌ์ด์ฆ 640์์ ์ค๋ฅ๊ฐ ์ฆ์์ 600์ผ๋ก ๋ค์ดํ์ฌ ํ์ต
- batch 8 : 16 ์ค์ ์ ๋ณด๋ค ์ข์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ 8๋ก ์ค์
- epochs : 130
- weights : yolov5m. ์ฒ์์๋ s๋ชจ๋ธ๋ก ์์ํ์์ผ๋ ์ข๋ ์ข์ ์ฑ๋ฅ์ ์ํด m ๋ชจ๋ธ๋ก ๋ณ๊ฒฝํ์ต
- data : yaml ํ์ผ์์น ์ค์ . (๋ผ๋ฒจ๋ง ์ ๋ณด์ ํ์ผ ๊ฒฝ๋ก)
!cd /content/yolov5; python train.py --img 600 --batch 8 --epochs 130 --data /content/gdrive/MyDrive/Project_Data/Tracycle_Train.yaml --weights yolov5m.pt \
--project=/mydrive/Project_Data --name summary --exist-ok
- ์์ค์ฝ๋ : https://github.com/ultralytics/yolov5
- models/yolov5m.yaml
# YOLOv5 ๐ by Ultralytics, GPL-3.0 license
# Parameters
nc: 80 # number of classes
depth_multiple: 0.67 # model depth multiple
width_multiple: 0.75 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Focus, [64, 3]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 9, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, C3, [1024, False]], # 9
]
# YOLOv5 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
- Yolo v5๋ ์ผ๋ฐ์ ์ธ Object Detection ๊ตฌ์ฑ๊ณผ ๊ฑฐ์ ๊ฐ์
- ํฌ๊ฒ Backbone ๊ณผ Head ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ
- ์ฐธ๊ณ : https://ropiens.tistory.com/44
- ์ฐธ๊ณ : https://blog.csdn.net/Q1u1NG/article/details/107511465