Initial Code Commit

This commit is contained in:
2025-10-24 02:07:59 -04:00
commit f099f36838
63 changed files with 4425 additions and 0 deletions

58
AI Training/ai.ipynb Executable file
View File

@@ -0,0 +1,58 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import dask.dataframe as dd"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" image label\n",
"0 {'bytes': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x... 0\n",
"1 {'bytes': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x... 0\n",
"2 {'bytes': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x... 0\n",
"3 {'bytes': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x... 0\n",
"4 {'bytes': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x... 0\n"
]
}
],
"source": [
"df = dd.read_parquet(\"hf://datasets/edwinpalegre/trashnet_enhanced/data/train-*.parquet\")\n",
"\n",
"print(df.show())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Binary file not shown.

6
AI Training/hf.py Executable file
View File

@@ -0,0 +1,6 @@
# Load model directly
from transformers import AutoImageProcessor, AutoModelForImageClassification
processor = AutoImageProcessor.from_pretrained("edwinpalegre/ee8225-group4-vit-trashnet-enhanced")
model = AutoModelForImageClassification.from_pretrained("edwinpalegre/ee8225-group4-vit-trashnet-enhanced")

96
AI Training/main.py Executable file
View File

@@ -0,0 +1,96 @@
import dask.dataframe as dd
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import os, time, uuid
ENDPOINT = "https://trashvision.cognitiveservices.azure.com/"
training_key = "611e786a785648e38f346f18e7f7e7ed"
prediction_key = "611e786a785648e38f346f18e7f7e7ed"
project_id = "a67f7d7b-c980-49bd-b57d-0bd1367b29d0"
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
df = dd.read_parquet("hf://datasets/edwinpalegre/trashnet_enhanced/data/train-*.parquet")
# ## iterate over the the first 5 rows of the dataframe and decode the image bytes to an image and save it to a file
tags = trainer.get_tags(project_id)
biodegradable_tag = None
cardboard_tag = None
glass_tag = None
metal_tag = None
paper_tag = None
plastic_tag = None
for tag in tags:
if tag.name == "biodegradable":
biodegradable_tag = tag
elif tag.name == "cardboard":
cardboard_tag = tag
elif tag.name == "glass":
glass_tag = tag
elif tag.name == "metal":
metal_tag = tag
elif tag.name == "paper":
paper_tag = tag
elif tag.name == "plastic":
plastic_tag = tag
print(biodegradable_tag)
print(cardboard_tag)
print(glass_tag)
print(metal_tag)
print(paper_tag)
print(plastic_tag)
# get all images from in the current dir and upload them to the custom vision project
# base_image_location = os.path.join (os.path.dirname(__file__), "images")
# tagged_images_with_regions = []
# for image in os.listdir(base_image_location):
# print(image)
# with open(os.path.join(base_image_location, image), "rb") as image_contents:
# trainer.create_images_from_data(project_id, image_contents.read(), [biodegradable_tag.id])
# print("Uploaded image: ", image)
# time.sleep(5)
skip = 10031
count = 0
for index, row in df.iterrows():
if count < skip:
count += 1
continue
else:
count += 1
image = row["image"]["bytes"]
label = row["label"]
if label == 0:
trainer.create_images_from_data(project_id, image, [biodegradable_tag.id])
elif label == 1:
trainer.create_images_from_data(project_id, image, [cardboard_tag.id])
elif label == 2:
trainer.create_images_from_data(project_id, image, [glass_tag.id])
elif label == 3:
trainer.create_images_from_data(project_id, image, [metal_tag.id])
elif label == 4:
trainer.create_images_from_data(project_id, image, [paper_tag.id])
elif label == 5:
trainer.create_images_from_data(project_id, image, [plastic_tag.id])
print(f"C: {count}, I: {index}, L: {label}, Uploaded image")
time.sleep(1)
print("Done uploading images")

39
AI Training/ml.py Executable file
View File

@@ -0,0 +1,39 @@
import dask.dataframe as dd
import os
df = dd.read_parquet("hf://datasets/edwinpalegre/trashnet_enhanced/data/train-*.parquet")
count = 0
for index, row in df.iterrows():
label = row["label"]
image = row["image"]["bytes"]
if label == 0:
with open(os.path.join("images", "biodegradable", f"biodegradable_{count}.jpg"), "wb") as f:
f.write(image)
elif label == 1:
with open(os.path.join("images", "cardboard", f"cardboard_{count}.jpg"), "wb") as f:
f.write(image)
elif label == 2:
with open(os.path.join("images", "glass", f"glass_{count}.jpg"), "wb") as f:
f.write(image)
elif label == 3:
with open(os.path.join("images", "metal", f"metal_{count}.jpg"), "wb") as f:
f.write(image)
elif label == 4:
with open(os.path.join("images", "paper", f"paper_{count}.jpg"), "wb") as f:
f.write(image)
elif label == 5:
with open(os.path.join("images", "plastic", f"plastic_{count}.jpg"), "wb") as f:
f.write(image)
else:
print("Label not found")
break
print(f"Saved image {count}")
count += 1
print("Done!")

29
AI Training/pdf.py Executable file
View File

@@ -0,0 +1,29 @@
from inference import get_model
import supervision as sv
import cv2
# define the image url to use for inference
image_file = "taylor-swift-album-1989.jpeg"
image = cv2.imread(image_file)
# load a pre-trained yolov8n model
model = get_model(model_id="taylor-swift-records/3")
# run inference on our chosen image, image can be a url, a numpy array, a PIL image, etc.
results = model.infer(image)[0]
# load the results into the supervision Detections api
detections = sv.Detections.from_inference(results)
# create supervision annotators
bounding_box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()
# annotate the image with our inference results
annotated_image = bounding_box_annotator.annotate(
scene=image, detections=detections)
annotated_image = label_annotator.annotate(
scene=annotated_image, detections=detections)
# display the image
sv.plot_image(annotated_image)

29
AI Training/train.py Executable file
View File

@@ -0,0 +1,29 @@
import dask.dataframe as dd
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import os, time, uuid
ENDPOINT = "https://trashvision.cognitiveservices.azure.com/"
training_key = "611e786a785648e38f346f18e7f7e7ed"
prediction_key = "611e786a785648e38f346f18e7f7e7ed"
project_id = "a67f7d7b-c980-49bd-b57d-0bd1367b29d0"
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
print ("Training...")
iteration = trainer.train_project(project_id)
while (iteration.status != "Completed"):
iteration = trainer.get_iteration(project_id, iteration.id)
print ("Training status: " + iteration.status)
time.sleep(1)
# The iteration is now trained. Publish it to the project endpoint
trainer.publish_iteration(project_id, iteration.id, "HaxNet1", predictor)
print ("Done!")