app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,115 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
-
import time
|
| 5 |
-
import os
|
| 6 |
-
import tkinter as tk
|
| 7 |
-
from PIL import Image
|
| 8 |
import json
|
| 9 |
from datasets import load_dataset
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
directory_path = load_dataset("HuberDa/Labeltest")
|
| 12 |
-
print("Spaltennamen:", directory_path['train'].column_names)
|
| 13 |
-
print("Erstes Beispiel im Train-Split:", directory_path['train'][0])
|
| 14 |
-
count = 0
|
| 15 |
|
|
|
|
| 16 |
def image_generator(hf_dataset, split="train"):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if image_column is None:
|
| 24 |
-
raise ValueError("Keine Bildspalte gefunden!")
|
| 25 |
-
|
| 26 |
-
# Schleife durch das Dataset und generiere Bild-Arrays
|
| 27 |
-
for example in hf_dataset[split]:
|
| 28 |
-
image = example[image_column] # Greife auf das Bild zu
|
| 29 |
-
image_array = np.array(image) # Konvertiere es in ein NumPy-Array
|
| 30 |
-
filename = example['id'] if 'id' in example else "unknown"
|
| 31 |
-
yield filename, image_array
|
| 32 |
-
|
| 33 |
-
# Create the generator
|
| 34 |
image_gen = image_generator(directory_path)
|
| 35 |
|
| 36 |
-
def record_input(fname:str, label:str):
|
| 37 |
-
if label != "Pass":
|
| 38 |
-
output = {"fname": fname, "label": label}
|
| 39 |
-
with open("output.txt", 'a') as f:
|
| 40 |
-
json.dump(output, f)
|
| 41 |
-
f.write("\n")
|
| 42 |
-
fname, image = next(image_gen)
|
| 43 |
-
return fname, image
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def start():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
demo.queue()
|
| 85 |
demo.launch(share=True, debug=True)
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
from datasets import load_dataset
|
| 5 |
|
| 6 |
+
|
| 7 |
+
theme = gr.themes.Base(
|
| 8 |
+
secondary_hue="cyan",
|
| 9 |
+
).set(
|
| 10 |
+
button_primary_background_fill='*primary_100',
|
| 11 |
+
button_primary_text_color='*neutral_950',
|
| 12 |
+
border_color_accent="*primary_100",
|
| 13 |
+
body_text_color="*neutral_950",
|
| 14 |
+
block_border_color="*primary_100",
|
| 15 |
+
block_title_text_color="*neutral_950",
|
| 16 |
+
block_title_text_size="text_xl",
|
| 17 |
+
panel_border_color="*primary_950",
|
| 18 |
+
form_gap_width="2px"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Daten laden
|
| 25 |
directory_path = load_dataset("HuberDa/Labeltest")
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Bilder generieren
|
| 28 |
def image_generator(hf_dataset, split="train"):
|
| 29 |
+
for index, example in enumerate(hf_dataset[split]):
|
| 30 |
+
image = example['image'] # Access the image column
|
| 31 |
+
image_array = np.array(image) # Convert to a NumPy array
|
| 32 |
+
yield index, image_array # Yield the index and image
|
| 33 |
+
|
| 34 |
+
# Generator aktivieren
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
image_gen = image_generator(directory_path)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Input der Bilder sammeln
|
| 39 |
+
def record_input(label: str, index: int, free_text: str):
|
| 40 |
+
# Den Output mit index, label, and free text sammeln
|
| 41 |
+
output = {
|
| 42 |
+
"index": index,
|
| 43 |
+
"label": label,
|
| 44 |
+
"free_text": free_text
|
| 45 |
+
}
|
| 46 |
+
# Output notieren
|
| 47 |
+
with open("output.txt", 'a') as f:
|
| 48 |
+
json.dump(output, f)
|
| 49 |
+
f.write("\n")
|
| 50 |
+
return f"Bild {index} wurde als {label} klassifiziert. \nZusätzliche Notizen: {free_text}"
|
| 51 |
|
| 52 |
def start():
|
| 53 |
+
try:
|
| 54 |
+
# Nächstes Bild und Index holen
|
| 55 |
+
index, image = next(image_gen) # Index und Bild entpacken
|
| 56 |
+
return str(index), image, "" # Rückgabe von index, image, and leeren der Textbox
|
| 57 |
+
except StopIteration:
|
| 58 |
+
return "No more images", None, "" # Falls keine Bilder übrig
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return str(e), None, "" # Für andere Probleme
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(fill_height=True, fill_width=True, theme = theme) as demo:
|
| 63 |
+
gr.Markdown("OCTis Training Center")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
# Bildanzeige
|
| 67 |
+
img_block = gr.Image(visible=True, width=500, height=500, show_fullscreen_button=True, show_share_button=False, show_download_button=False, show_label=False)
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
output_message = gr.Textbox(label="Ergebnis", interactive=False)
|
| 71 |
+
free_text_input = gr.Textbox(label="zusätzliche Informationen", placeholder="Hier Text eingeben...", lines=2)
|
| 72 |
+
|
| 73 |
+
# versteckte textbox für index
|
| 74 |
+
index_box = gr.Textbox(label="Index", visible=False)
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
# Buttons 1 - 3
|
| 78 |
+
disease1_btn = gr.Button(value="Disease 1",variant="primary")
|
| 79 |
+
disease2_btn = gr.Button(value="Disease 2",variant="primary")
|
| 80 |
+
disease3_btn = gr.Button(value="Disease 3",variant="primary")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
# Button 4 - 6
|
| 84 |
+
disease4_btn = gr.Button(value="Disease 4",variant="primary")
|
| 85 |
+
disease5_btn = gr.Button(value="Disease 5",variant="primary")
|
| 86 |
+
nodisease_btn = gr.Button(value="No Disease",variant="primary")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
# Start und skip Button
|
| 90 |
+
start_btn = gr.Button("Start")
|
| 91 |
+
skip_btn = gr.Button("Überspringen")
|
| 92 |
+
|
| 93 |
+
# Starten der App
|
| 94 |
+
start_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 95 |
+
skip_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 96 |
+
|
| 97 |
+
# Labeling mit Buttons
|
| 98 |
+
disease1_btn.click(fn=lambda idx, free_text: record_input("Disease 1", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 99 |
+
disease2_btn.click(fn=lambda idx, free_text: record_input("Disease 2", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 100 |
+
disease3_btn.click(fn=lambda idx, free_text: record_input("Disease 3", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 101 |
+
disease4_btn.click(fn=lambda idx, free_text: record_input("Disease 4", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 102 |
+
disease5_btn.click(fn=lambda idx, free_text: record_input("Disease 5", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 103 |
+
nodisease_btn.click(fn=lambda idx, free_text: record_input("No Disease", idx, free_text), inputs=[index_box, free_text_input], outputs=output_message)
|
| 104 |
+
|
| 105 |
+
# Nächstes Bild
|
| 106 |
+
disease1_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 107 |
+
disease2_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 108 |
+
disease3_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 109 |
+
disease4_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 110 |
+
disease5_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 111 |
+
nodisease_btn.click(fn=start, outputs=[index_box, img_block, free_text_input])
|
| 112 |
+
|
| 113 |
|
| 114 |
demo.queue()
|
| 115 |
demo.launch(share=True, debug=True)
|