Skip to content

Commit

Permalink
Merge pull request #6 from konradbachusz/feature/konrad-recommendatio…
Browse files Browse the repository at this point in the history
…ns-and-training

Feature/konrad recommendations and training
  • Loading branch information
konradbachusz authored Jun 2, 2024
2 parents 6828fad + 690595c commit dd463d0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 33 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
streamlit==1.3.0
streamlit==1.35.0
transformers==4.29.2
protobuf==3.20.0
altair==4.0
Expand Down
35 changes: 14 additions & 21 deletions src/camera_on.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
import streamlit as st
import numpy as np
from PIL import Image
import cv2
from logo import display_logo
from recommendations import get_recommendations



def show_camera_on():

display_logo()
st.header("Camera On")
capture = st.button('Capture from Camera')
img_file_buffer = st.camera_input("'Capture from Camera'")

if capture:
# Open the camera
cap = cv2.VideoCapture(0)
if img_file_buffer is not None:
# To read image file buffer as a PIL Image:
image = Image.open(img_file_buffer)

if not cap.isOpened():
st.error("Could not open camera.")
else:
ret, frame = cap.read()
cap.release() # Release the camera immediately after capturing
# To convert PIL Image to numpy array:
img_array = np.array(image)

if ret:
# Convert the frame to RGB (cv2 captures in BGR)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
st.image(image, caption='Uploaded Image', use_column_width=True)

# Convert the frame to PIL image
img = Image.fromarray(frame)

#TODO Pass the image to the model and return the predicted label e.g "scarf", "shirt" etc.
predicted_label = "Shirt" #TODO: This is a mock. Please change this to an actual prediction

# Convert the PIL image to numpy array
img_array = np.array(img)
get_recommendations(predicted_label)

st.image(img, caption='Captured Image', use_column_width=True)
else:
st.error("Failed to capture image.")


if __name__ == "__main__":
Expand Down
125 changes: 114 additions & 11 deletions src/cnn_training_rav.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "i4Vqn5oayGjh",
"outputId": "684c8846-97ca-4517-dd2f-96eac3c7afcd"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: kaggle in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (1.6.14)\n",
"Requirement already satisfied: python-dateutil in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (2.9.0.post0)\n",
"Requirement already satisfied: tqdm in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (4.66.4)\n",
"Requirement already satisfied: python-slugify in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (8.0.4)\n",
"Requirement already satisfied: requests in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (2.32.3)\n",
"Requirement already satisfied: bleach in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (6.1.0)\n",
"Requirement already satisfied: urllib3 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (2.2.1)\n",
"Requirement already satisfied: six>=1.10 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (1.16.0)\n",
"Requirement already satisfied: certifi>=2023.7.22 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from kaggle) (2024.2.2)\n",
"Requirement already satisfied: colorama; platform_system == \"Windows\" in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from tqdm->kaggle) (0.4.6)\n",
"Requirement already satisfied: text-unidecode>=1.3 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from python-slugify->kaggle) (1.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from requests->kaggle) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from requests->kaggle) (3.7)\n",
"Requirement already satisfied: webencodings in c:\\users\\konrad\\documents\\projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages (from bleach->kaggle) (0.5.1)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: You are using pip version 20.1.1; however, version 24.0 is available.\n",
"You should consider upgrading via the 'c:\\Users\\Konrad\\Documents\\Projects\\oxford-ai-summit-hackathon-group-4\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
]
}
],
"source": [
"%pip install kaggle"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {
"id": "pk4SACVlb6xT"
},
Expand All @@ -39,7 +69,7 @@
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n",
"from tensorflow.keras.callbacks import ModelCheckpoint\n",
"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"from sklearn.model_selection import train_test_split\n",
Expand All @@ -49,15 +79,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zzngwmYNx6NO",
"outputId": "68447921-6110-4757-cf8c-7b1863d13ea7"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on GPU or CPU\n"
]
}
],
"source": [
"# Enable TPU if available\n",
"try:\n",
Expand All @@ -73,15 +111,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "X8ye6k36lXxA",
"outputId": "e5e8206c-44e3-4a73-8ed5-9e146139a270"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset URL: https://www.kaggle.com/datasets/paramaggarwal/fashion-product-images-dataset\n"
]
},
{
"ename": "OSError",
"evalue": "[Errno 28] No space left on device",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mOSError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[19], line 6\u001b[0m\n\u001b[0;32m 3\u001b[0m dataset \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mparamaggarwal/fashion-product-images-dataset\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m 4\u001b[0m destination_folder \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mfashion_product_images\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m----> 6\u001b[0m api\u001b[39m.\u001b[39;49mdataset_download_files(dataset, path\u001b[39m=\u001b[39;49mdestination_folder, unzip\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n",
"File \u001b[1;32mc:\\Users\\Konrad\\Documents\\Projects\\oxford-ai-summit-hackathon-group-4\\venv\\lib\\site-packages\\kaggle\\api\\kaggle_api_extended.py:1509\u001b[0m, in \u001b[0;36mKaggleApi.dataset_download_files\u001b[1;34m(self, dataset, path, force, quiet, unzip, licenses)\u001b[0m\n\u001b[0;32m 1507\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 1508\u001b[0m \u001b[39mwith\u001b[39;00m zipfile\u001b[39m.\u001b[39mZipFile(outfile) \u001b[39mas\u001b[39;00m z:\n\u001b[1;32m-> 1509\u001b[0m z\u001b[39m.\u001b[39;49mextractall(effective_path)\n\u001b[0;32m 1510\u001b[0m \u001b[39mexcept\u001b[39;00m zipfile\u001b[39m.\u001b[39mBadZipFile \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 1511\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[0;32m 1512\u001b[0m \u001b[39m'\u001b[39m\u001b[39mBad zip file, please report on \u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m 1513\u001b[0m \u001b[39m'\u001b[39m\u001b[39mwww.github.com/kaggle/kaggle-api\u001b[39m\u001b[39m'\u001b[39m, e)\n",
"File \u001b[1;32m~\\anaconda3\\lib\\zipfile.py:1647\u001b[0m, in \u001b[0;36mZipFile.extractall\u001b[1;34m(self, path, members, pwd)\u001b[0m\n\u001b[0;32m 1644\u001b[0m path \u001b[39m=\u001b[39m os\u001b[39m.\u001b[39mfspath(path)\n\u001b[0;32m 1646\u001b[0m \u001b[39mfor\u001b[39;00m zipinfo \u001b[39min\u001b[39;00m members:\n\u001b[1;32m-> 1647\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_extract_member(zipinfo, path, pwd)\n",
"File \u001b[1;32m~\\anaconda3\\lib\\zipfile.py:1702\u001b[0m, in \u001b[0;36mZipFile._extract_member\u001b[1;34m(self, member, targetpath, pwd)\u001b[0m\n\u001b[0;32m 1698\u001b[0m \u001b[39mreturn\u001b[39;00m targetpath\n\u001b[0;32m 1700\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mopen(member, pwd\u001b[39m=\u001b[39mpwd) \u001b[39mas\u001b[39;00m source, \\\n\u001b[0;32m 1701\u001b[0m \u001b[39mopen\u001b[39m(targetpath, \u001b[39m\"\u001b[39m\u001b[39mwb\u001b[39m\u001b[39m\"\u001b[39m) \u001b[39mas\u001b[39;00m target:\n\u001b[1;32m-> 1702\u001b[0m shutil\u001b[39m.\u001b[39;49mcopyfileobj(source, target)\n\u001b[0;32m 1704\u001b[0m \u001b[39mreturn\u001b[39;00m targetpath\n",
"File \u001b[1;32m~\\anaconda3\\lib\\shutil.py:205\u001b[0m, in \u001b[0;36mcopyfileobj\u001b[1;34m(fsrc, fdst, length)\u001b[0m\n\u001b[0;32m 203\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m buf:\n\u001b[0;32m 204\u001b[0m \u001b[39mbreak\u001b[39;00m\n\u001b[1;32m--> 205\u001b[0m fdst_write(buf)\n",
"\u001b[1;31mOSError\u001b[0m: [Errno 28] No space left on device"
]
}
],
"source": [
"api = KaggleApi()\n",
"\n",
Expand Down Expand Up @@ -245,15 +306,57 @@
"id": "gw-fLn0Mb6xV",
"outputId": "2c034cf5-4db1-4f81-c7e3-c994b67aa7cc"
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<keras.src.callbacks.EarlyStopping at 0x208d51268e0>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"callback = EarlyStopping(\n",
" monitor=\"categorical_crossentropy\",\n",
" min_delta=0,\n",
" patience=3,\n",
" verbose=0,\n",
" mode=\"auto\",\n",
" baseline=None,\n",
" restore_best_weights=False,\n",
" start_from_epoch=0,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'model' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m# Train the model\u001b[39;00m\n\u001b[0;32m 2\u001b[0m checkpoint \u001b[39m=\u001b[39m ModelCheckpoint(\u001b[39m'\u001b[39m\u001b[39mfashion_mnist_model.keras\u001b[39m\u001b[39m'\u001b[39m, save_best_only\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n\u001b[1;32m----> 3\u001b[0m history \u001b[39m=\u001b[39m model\u001b[39m.\u001b[39mfit(\n\u001b[0;32m 4\u001b[0m train_generator,\n\u001b[0;32m 5\u001b[0m epochs\u001b[39m=\u001b[39m\u001b[39m10\u001b[39m,\n\u001b[0;32m 6\u001b[0m validation_data\u001b[39m=\u001b[39mval_generator,\n\u001b[0;32m 7\u001b[0m callbacks\u001b[39m=\u001b[39m[checkpoint],\n\u001b[0;32m 8\u001b[0m )\n",
"\u001b[1;31mNameError\u001b[0m: name 'model' is not defined"
]
}
],
"source": [
"\n",
"# Train the model\n",
"checkpoint = ModelCheckpoint('fashion_mnist_model.keras', save_best_only=True)\n",
"history = model.fit(\n",
" train_generator,\n",
" epochs=2,\n",
" epochs=10,\n",
" validation_data=val_generator,\n",
" callbacks=[checkpoint],\n",
" callbacks=[checkpoint, callback],\n",
")"
]
},
Expand Down

0 comments on commit dd463d0

Please sign in to comment.