Spaces:
Sleeping
Sleeping
Commit
·
195fe71
1
Parent(s):
b2ae262
- app.py +198 -0
- requirements.txt +2 -0
- uploaded_images/94.jpg +0 -0
- uploaded_images/95.jpg +0 -0
- uploaded_images/96.jpg +0 -0
- uploaded_images/labels.json +1 -0
- uploaded_images/model.pkl +3 -0
- uploaded_images/sample_dataset.json +1 -0
- uploaded_images/tamil-woman-close-up-of-happy-face-ECNPHF.jpg +0 -0
- uploaded_images/ung-cheerful-beautiful-girl-long-hair-casual-shirt-smiling-looking-172927805.jpg +0 -0
- uploaded_images/vladimir-putin-smiling-face-png-11646750895sk2xyu6id1.png +0 -0
- uploaded_images/web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg +0 -0
app.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import numpy as np
|
6 |
+
import pickle
|
7 |
+
from sklearn.ensemble import RandomForestClassifier
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
from sklearn.metrics import accuracy_score
|
10 |
+
|
11 |
+
# Directories
|
12 |
+
UPLOAD_DIR = "uploaded_images"
|
13 |
+
MODEL_PATH = os.path.join(UPLOAD_DIR, "model.pkl")
|
14 |
+
LABELS_PATH = os.path.join(UPLOAD_DIR, "labels.json")
|
15 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
16 |
+
|
17 |
+
# Initialize model and labels
|
18 |
+
if os.path.exists(MODEL_PATH):
|
19 |
+
with open(MODEL_PATH, "rb") as f:
|
20 |
+
model = pickle.load(f)
|
21 |
+
else:
|
22 |
+
model = RandomForestClassifier()
|
23 |
+
|
24 |
+
if os.path.exists(LABELS_PATH):
|
25 |
+
with open(LABELS_PATH, "r") as f:
|
26 |
+
labels = json.load(f)
|
27 |
+
else:
|
28 |
+
labels = {}
|
29 |
+
|
30 |
+
# Helper function to save uploaded images
|
31 |
+
def save_uploaded_image(uploaded_file):
|
32 |
+
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
33 |
+
with open(file_path, "wb") as f:
|
34 |
+
f.write(uploaded_file.getbuffer())
|
35 |
+
return file_path
|
36 |
+
|
37 |
+
# Feature extraction function
|
38 |
+
def extract_features(file_path):
|
39 |
+
try:
|
40 |
+
with Image.open(file_path) as img:
|
41 |
+
return np.array(img.resize((64, 64))).flatten()
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Error processing image {file_path}: {e}")
|
44 |
+
return None
|
45 |
+
|
46 |
+
# Train the model
|
47 |
+
def train_model(training_data):
|
48 |
+
global model, labels
|
49 |
+
|
50 |
+
features, targets = [], []
|
51 |
+
for file_path, label in training_data.items():
|
52 |
+
feature = extract_features(file_path)
|
53 |
+
if feature is not None:
|
54 |
+
features.append(feature)
|
55 |
+
targets.append(label)
|
56 |
+
|
57 |
+
if features:
|
58 |
+
features = np.array(features)
|
59 |
+
targets = np.array(targets)
|
60 |
+
|
61 |
+
model.fit(features, targets)
|
62 |
+
|
63 |
+
with open(MODEL_PATH, "wb") as f:
|
64 |
+
pickle.dump(model, f)
|
65 |
+
|
66 |
+
with open(LABELS_PATH, "w") as f:
|
67 |
+
json.dump(labels, f)
|
68 |
+
else:
|
69 |
+
print("No valid features found for training.")
|
70 |
+
|
71 |
+
# Classify an image
|
72 |
+
def classify_image(file_path):
|
73 |
+
global model
|
74 |
+
features = extract_features(file_path)
|
75 |
+
if features is not None:
|
76 |
+
features = features.reshape(1, -1)
|
77 |
+
return model.predict(features)[0]
|
78 |
+
else:
|
79 |
+
return "Invalid Image"
|
80 |
+
|
81 |
+
# Streamlit app
|
82 |
+
def main():
|
83 |
+
st.title("Human or Alien Identification")
|
84 |
+
|
85 |
+
st.markdown(
|
86 |
+
"""
|
87 |
+
Welcome to the **Human or Alien Identification App**! Here's what you can do:
|
88 |
+
|
89 |
+
- **Identify Image:** Upload an image and classify it as "Human" or "Alien." The classifications you save will be added to the training data.
|
90 |
+
- **Train Model:** Review and manage the images already classified as "Human" or "Alien." Upload additional images to improve the training dataset.
|
91 |
+
"""
|
92 |
+
)
|
93 |
+
|
94 |
+
tab1, tab2 = st.tabs(["Identify Image", "Train Model"])
|
95 |
+
|
96 |
+
with tab1:
|
97 |
+
st.header("Identify Image")
|
98 |
+
uploaded_files = st.file_uploader("Upload Images to Identify", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
99 |
+
|
100 |
+
if uploaded_files:
|
101 |
+
results = {}
|
102 |
+
|
103 |
+
for uploaded_file in uploaded_files:
|
104 |
+
try:
|
105 |
+
st.image(uploaded_file, caption=f"Uploaded Image: {uploaded_file.name}", use_container_width=True)
|
106 |
+
file_path = save_uploaded_image(uploaded_file)
|
107 |
+
|
108 |
+
prediction = classify_image(file_path)
|
109 |
+
results[file_path] = prediction
|
110 |
+
except Exception as e:
|
111 |
+
st.error(f"Error processing file {uploaded_file.name}: {e}")
|
112 |
+
|
113 |
+
st.subheader("Classification Results")
|
114 |
+
for file_path, label in results.items():
|
115 |
+
st.markdown(
|
116 |
+
f"<p style='font-size:20px; color:blue; background-color:lightyellow; padding:10px; border-radius:5px;'>Image: {os.path.basename(file_path)} - Classified as: {label}</p>",
|
117 |
+
unsafe_allow_html=True,
|
118 |
+
)
|
119 |
+
|
120 |
+
with tab2:
|
121 |
+
st.header("Train Model")
|
122 |
+
|
123 |
+
st.subheader("Upload Images for Training")
|
124 |
+
training_files = st.file_uploader("Upload Training Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
125 |
+
|
126 |
+
if training_files:
|
127 |
+
training_data = {}
|
128 |
+
|
129 |
+
for training_file in training_files:
|
130 |
+
try:
|
131 |
+
st.image(training_file, caption=f"Uploaded Training Image: {training_file.name}", use_container_width=True)
|
132 |
+
file_path = save_uploaded_image(training_file)
|
133 |
+
|
134 |
+
label = st.radio(
|
135 |
+
f"Classify {training_file.name}", ["Human", "Alien"], index=0, key=training_file.name
|
136 |
+
)
|
137 |
+
training_data[file_path] = label
|
138 |
+
except Exception as e:
|
139 |
+
st.error(f"Error processing file {training_file.name}: {e}")
|
140 |
+
|
141 |
+
if st.button("Save Training Data"):
|
142 |
+
labels.update(training_data)
|
143 |
+
train_model(training_data)
|
144 |
+
st.success("Training data has been saved and the model updated.")
|
145 |
+
|
146 |
+
st.subheader("Bulk Train with Existing Dataset")
|
147 |
+
dataset_file = st.file_uploader("Upload a JSON file containing labeled data", type=["json"])
|
148 |
+
|
149 |
+
if dataset_file:
|
150 |
+
dataset = json.load(dataset_file)
|
151 |
+
training_data = {}
|
152 |
+
|
153 |
+
for label, image_paths in dataset.items():
|
154 |
+
for image_path in image_paths:
|
155 |
+
if os.path.exists(image_path):
|
156 |
+
training_data[image_path] = label
|
157 |
+
|
158 |
+
if st.button("Train Model with Dataset"):
|
159 |
+
labels.update(training_data)
|
160 |
+
train_model(training_data)
|
161 |
+
st.success("Model has been trained with the uploaded dataset.")
|
162 |
+
|
163 |
+
subtab1, subtab2 = st.tabs(["Humans", "Aliens"])
|
164 |
+
|
165 |
+
with subtab1:
|
166 |
+
st.subheader("Human Images")
|
167 |
+
human_images = [
|
168 |
+
img for img, lbl in labels.items() if lbl == "Human"
|
169 |
+
]
|
170 |
+
|
171 |
+
if human_images:
|
172 |
+
st.info("These images have already been trained.")
|
173 |
+
for image_path in human_images:
|
174 |
+
try:
|
175 |
+
st.image(image_path, caption=f"Image: {os.path.basename(image_path)}", use_container_width=True)
|
176 |
+
except Exception as e:
|
177 |
+
st.error(f"Error displaying image {os.path.basename(image_path)}: {e}")
|
178 |
+
else:
|
179 |
+
st.warning("No human images found for training.")
|
180 |
+
|
181 |
+
with subtab2:
|
182 |
+
st.subheader("Alien Images")
|
183 |
+
alien_images = [
|
184 |
+
img for img, lbl in labels.items() if lbl == "Alien"
|
185 |
+
]
|
186 |
+
|
187 |
+
if alien_images:
|
188 |
+
st.info("These images have already been trained.")
|
189 |
+
for image_path in alien_images:
|
190 |
+
try:
|
191 |
+
st.image(image_path, caption=f"Image: {os.path.basename(image_path)}", use_container_width=True)
|
192 |
+
except Exception as e:
|
193 |
+
st.error(f"Error displaying image {os.path.basename(image_path)}: {e}")
|
194 |
+
else:
|
195 |
+
st.warning("No alien images found for training.")
|
196 |
+
|
197 |
+
if __name__ == "__main__":
|
198 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pillow
|
uploaded_images/94.jpg
ADDED
![]() |
uploaded_images/95.jpg
ADDED
![]() |
uploaded_images/96.jpg
ADDED
![]() |
uploaded_images/labels.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"uploaded_images\\tamil-woman-close-up-of-happy-face-ECNPHF.jpg": "Human", "uploaded_images\\ung-cheerful-beautiful-girl-long-hair-casual-shirt-smiling-looking-172927805.jpg": "Human", "uploaded_images\\vladimir-putin-smiling-face-png-11646750895sk2xyu6id1.png": "Human", "uploaded_images\\web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg": "Human", "uploaded_images\\94.jpg": "Alien", "uploaded_images\\95.jpg": "Alien", "uploaded_images\\96.jpg": "Alien"}
|
uploaded_images/model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:425e5e787d6520e1c913c617f7eadb38c4d7eabcd3df9444e9ec07dd9cae70c7
|
3 |
+
size 55529
|
uploaded_images/sample_dataset.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"Human": ["uploaded_images/sample_human1.jpg", "uploaded_images/sample_human2.jpg"], "Alien": ["uploaded_images/sample_alien1.jpg", "uploaded_images/sample_alien2.jpg"]}
|
uploaded_images/tamil-woman-close-up-of-happy-face-ECNPHF.jpg
ADDED
![]() |
uploaded_images/ung-cheerful-beautiful-girl-long-hair-casual-shirt-smiling-looking-172927805.jpg
ADDED
![]() |
uploaded_images/vladimir-putin-smiling-face-png-11646750895sk2xyu6id1.png
ADDED
![]() |
uploaded_images/web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg
ADDED
![]() |