Upload 4 files
Browse files- app.py +90 -0
- detector_yolo.py +20 -0
- train_yolo.py +13 -0
- yolov8n.pt +3 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, glob
|
2 |
+
from flask import Flask, request, render_template
|
3 |
+
from transformers import pipeline
|
4 |
+
from ultralytics import YOLO
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
8 |
+
app.config['MARKED_FOLDER'] = 'static/marked'
|
9 |
+
PREDICT_FOLDER = os.path.join(app.config['MARKED_FOLDER'], 'predict')
|
10 |
+
|
11 |
+
# สร้างโฟลเดอร์หากยังไม่มี
|
12 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
13 |
+
os.makedirs(PREDICT_FOLDER, exist_ok=True)
|
14 |
+
|
15 |
+
# โหลดโมเดล
|
16 |
+
pipe = pipeline("image-classification", model="JorgeGIT/Finetuned-Leukemia-cell")
|
17 |
+
yolo_model = YOLO("runs/detect/leukemia-yolo-model/weights/best.pt")
|
18 |
+
|
19 |
+
# 🎯 ฟังก์ชันรวมความเสี่ยง
|
20 |
+
def calculate_combined_risk(blast_risk, huggingface_score, age, sex):
|
21 |
+
score = 0.6 * blast_risk + 0.4 * huggingface_score
|
22 |
+
if age and int(age) > 60:
|
23 |
+
score += 5
|
24 |
+
if sex == 'male':
|
25 |
+
score += 2
|
26 |
+
return min(round(score, 1), 100)
|
27 |
+
|
28 |
+
@app.route('/', methods=['GET'])
|
29 |
+
def home():
|
30 |
+
return render_template('index.html')
|
31 |
+
|
32 |
+
@app.route('/upload', methods=['GET', 'POST'])
|
33 |
+
def upload_file():
|
34 |
+
if request.method == 'POST':
|
35 |
+
file = request.files.get('file')
|
36 |
+
age = request.form.get('age')
|
37 |
+
sex = request.form.get('sex')
|
38 |
+
|
39 |
+
if not file or file.filename == '' or not age or not sex:
|
40 |
+
return "กรุณากรอกข้อมูลให้ครบ"
|
41 |
+
|
42 |
+
filename = file.filename
|
43 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
44 |
+
file.save(filepath)
|
45 |
+
|
46 |
+
results = yolo_model.predict(
|
47 |
+
source=filepath,
|
48 |
+
save=True,
|
49 |
+
project=app.config['MARKED_FOLDER'],
|
50 |
+
name='predict',
|
51 |
+
exist_ok=True
|
52 |
+
)
|
53 |
+
|
54 |
+
list_of_files = glob.glob(os.path.join(PREDICT_FOLDER, '*.jpg'))
|
55 |
+
latest_file = max(list_of_files, key=os.path.getctime)
|
56 |
+
marked_filename = os.path.relpath(latest_file, 'static').replace('\\', '/')
|
57 |
+
|
58 |
+
total_cells = len(results[0].boxes)
|
59 |
+
abnormal_cells = 0
|
60 |
+
for box in results[0].boxes:
|
61 |
+
cls_id = int(box.cls[0].item())
|
62 |
+
class_name = results[0].names[cls_id]
|
63 |
+
if 'blast' in class_name.lower():
|
64 |
+
abnormal_cells += 1
|
65 |
+
blast_risk = round((abnormal_cells / total_cells) * 100, 1) if total_cells else 0.0
|
66 |
+
|
67 |
+
result = pipe(filepath)
|
68 |
+
lla_result = [
|
69 |
+
{'label': 'โอกาสการเกิดโรค Leukemia = ', 'score': round(res['score'] * 1000, 1)}
|
70 |
+
for res in result if res['label'].lower() == 'lla'
|
71 |
+
]
|
72 |
+
huggingface_score = lla_result[0]['score'] if lla_result else 0.0
|
73 |
+
|
74 |
+
combined_risk = calculate_combined_risk(blast_risk, huggingface_score, age, sex)
|
75 |
+
|
76 |
+
return render_template('result.html',
|
77 |
+
filename=filename,
|
78 |
+
marked_filename=marked_filename,
|
79 |
+
total_cells=total_cells,
|
80 |
+
abnormal_cells=abnormal_cells,
|
81 |
+
blast_risk=blast_risk,
|
82 |
+
huggingface_score=huggingface_score,
|
83 |
+
combined_risk=combined_risk,
|
84 |
+
age=age,
|
85 |
+
sex=sex
|
86 |
+
)
|
87 |
+
|
88 |
+
return render_template('upload.html')
|
89 |
+
if __name__ == '__main__':
|
90 |
+
app.run(debug=True)
|
detector_yolo.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# detector_yolo.py
|
2 |
+
import os, glob
|
3 |
+
from ultralytics import YOLO
|
4 |
+
|
5 |
+
# โหลดโมเดล YOLO ที่เทรนเอง
|
6 |
+
yolo_model = YOLO("runs/detect/leukemia-yolo-model/weights/best.pt")
|
7 |
+
|
8 |
+
def detect_cells_yolo(image_path, output_dir='static/marked/predict'):
|
9 |
+
# สร้างโฟลเดอร์ถ้ายังไม่มี
|
10 |
+
os.makedirs(output_dir, exist_ok=True)
|
11 |
+
|
12 |
+
# ตรวจจับและบันทึกภาพ
|
13 |
+
yolo_model.predict(source=image_path, save=True, project='static/marked', name='predict', exist_ok=True)
|
14 |
+
|
15 |
+
# หาภาพล่าสุดที่เพิ่งสร้าง
|
16 |
+
list_of_files = glob.glob(os.path.join(output_dir, '*.jpg'))
|
17 |
+
if not list_of_files:
|
18 |
+
return None
|
19 |
+
latest_file = max(list_of_files, key=os.path.getctime)
|
20 |
+
return os.path.relpath(latest_file, 'static')
|
train_yolo.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
|
3 |
+
# โหลดโมเดล YOLOv8 เปล่า
|
4 |
+
model = YOLO("yolov8n.pt") # หรือ yolov8s.pt ถ้าเครื่องแรงขึ้น
|
5 |
+
|
6 |
+
# เทรนโมเดลใหม่ด้วย dataset ที่โหลดมา
|
7 |
+
model.train(
|
8 |
+
data="leukemia-detection/data.yaml", # path ไปยัง dataset.yaml
|
9 |
+
epochs=50,
|
10 |
+
imgsz=640,
|
11 |
+
batch=8,
|
12 |
+
name="leukemia-yolo-model",
|
13 |
+
)
|
yolov8n.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
3 |
+
size 6549796
|