Add files via upload

This commit is contained in:
Vita Aeterna 2023-01-05 00:24:26 +01:00 committed by GitHub
parent f86c327054
commit 73675480b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

224
ImageUsing.ipynb Normal file
View File

@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "50cc3f9e",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"from tqdm import tqdm\n",
"\n",
"# Directory to Pictures\n",
"DATA_DIR = \"C:/Users/muhle/Desktop/Code/DataSets/PetAI\"\n",
"# Kategorien die zu erkennen sind\n",
"CATEGORIES = [\"DOG\", \"CAT\"]\n",
"\n",
"# Die Ordner mit den Kategorien im Namen Suchen, anschließend alle Bilder in Schwarz-Weiß umformen \n",
"for category in CATEGORIES:\n",
" path = os.path.join(DATA_DIR, category)\n",
" for img in os.listdir(path):\n",
" img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)\n",
" # Anzeigen eines Bildes mit Matplotlib\n",
" plt.imshow(img_array, cmap=\"gray\")\n",
" plt.show()\n",
" break\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7847ac85",
"metadata": {},
"outputs": [],
"source": [
"# Größe des Bildes definieren\n",
"IMG_SIZE = 50\n",
"# Größe des Bild Array's ändern\n",
"new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n",
"# Neues verpixeltes Bild anzeigen\n",
"plt.imshow(new_array, cmap = 'gray')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "709419fa",
"metadata": {},
"outputs": [],
"source": [
"# Liste für Trainingsdaten definieren\n",
"training_data = []\n",
"\n",
"def create_training_data():\n",
" for category in CATEGORIES: # do dogs and cats\n",
"\n",
" path = os.path.join(DATA_DIR,category) # Pfad zu Hunde und Katzen erstellen\n",
" class_num = CATEGORIES.index(category) # Klassifikation für Hund und Katze bekommen, Hund=0; Katze=1;\n",
"\n",
" for img in tqdm(os.listdir(path)): # Bilder von Katzen und Hunden wiederholen\n",
" try:\n",
" img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # Bild zu einen Array\n",
" new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # Größe zu einer normalen Datengröße machen (50,50)\n",
" training_data.append([new_array, class_num]) # Hinzufügen zu unserer Liste trainings_data\n",
" except Exception as e: # Wenn ein Fehler auftritt soll es einfach übersrpingen und weitermachen\n",
" pass\n",
"create_training_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41c1245c",
"metadata": {},
"outputs": [],
"source": [
"# Länge der training_data\n",
"print(len(training_data))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdfe3e93",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"# Random Werte aus training_data nehmen\n",
"random.shuffle(training_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1e9c8e5",
"metadata": {},
"outputs": [],
"source": [
"# Array ausgeben\n",
"for sample in training_data[:10]:\n",
" print(sample[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5eaae186",
"metadata": {},
"outputs": [],
"source": [
"#Input (X) und Output (y) definieren\n",
"X = []\n",
"y = []\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "510602f8",
"metadata": {},
"outputs": [],
"source": [
"\n",
"for features, label in training_data: \n",
" X.append(features)\n",
" y.append(label)\n",
" \n",
"# X in einen Numpy Array umwandeln und ihn eine neue Größe geben, 1 Farmkanal (Grau)\n",
"X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b59339a3",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"# X und y in Pickle dateien schreiben\n",
"pickle_out = open(\"X.pickle\", \"wb\")\n",
"pickle.dump(X, pickle_out)\n",
"pickle_out.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b487159",
"metadata": {},
"outputs": [],
"source": [
"pickle_out = open(\"y.pickle\", \"wb\")\n",
"pickle.dump(y, pickle_out)\n",
"pickle_out.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6aabb96e",
"metadata": {},
"outputs": [],
"source": [
"pickle_out = open(\"X.pickle\", \"wb\")\n",
"pickle.dump(X, pickle_out)\n",
"pickle_out.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f0e4ca0",
"metadata": {},
"outputs": [],
"source": [
"X[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7532702",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ff3800d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}