tahamajs's picture
download
raw
4.13 kB
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4a00e22e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: opencv-python in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (4.6.0.66)\r\n",
"Requirement already satisfied: numpy>=1.19.3 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from opencv-python) (1.23.3)\r\n"
]
}
],
"source": [
"!pip3 install opencv-python"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f99438fe",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import glob\n",
"import os\n",
"import numpy as np\n",
"\n",
"# images = [cv2.imread(file) for file in glob.glob('./image/Images/*.jpg')]\n",
"\n",
"folder = './image/Images'\n",
"Label = {'Manchester': 0, 'Chelsea' : 1}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b91567c9",
"metadata": {},
"outputs": [],
"source": [
"labels = np.zeros(0, dtype=int, order='C')\n",
"predicted = np.zeros(0, dtype=int, order='C')\n",
"\n",
"red = [255, 0, 0] #label for Manchester\n",
"blue = [0, 0, 255] #label for Chelsea\n",
"\n",
"images = []\n",
"\n",
"for index,filename in enumerate(os.listdir(folder)):\n",
" img = cv2.imread(os.path.join(folder,filename))\n",
" if img is not None:\n",
" images.append(img)\n",
" \n",
" #set labels on pic\n",
" if filename[0]=='c':\n",
" labels = np.append(labels, Label['Chelsea'])\n",
" else:\n",
" labels = np.append(labels, Label['Manchester'])\n",
" \n",
" #mean colors of pic \n",
" color = np.mean(img, axis=(0, 1))\n",
" \n",
" #calc distance of each pic from red and blue\n",
" distance_from_blue = np.linalg.norm(color-blue)\n",
" distance_from_red = np.linalg.norm(color-red)\n",
" \n",
" #classify \n",
" if distance_from_blue > distance_from_red:\n",
" predicted = np.append(predicted, Label['Chelsea'])\n",
" else:\n",
" predicted = np.append(predicted, Label['Manchester'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8523a04e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 0.844262\n",
"Precision: 0.978723\n",
"Recall: 0.718750\n",
"F1 score: 0.828829\n"
]
},
{
"data": {
"text/plain": [
"array([[57, 1],\n",
" [18, 46]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
"\n",
"# accuracy: (tp + tn) / (p + n)\n",
"accuracy = accuracy_score(labels,predicted)\n",
"print('Accuracy: %f' % accuracy)\n",
"\n",
"# precision tp / (tp + fp)\n",
"precision = precision_score(labels,predicted)\n",
"print('Precision: %f' % precision)\n",
"\n",
"# recall: tp / (tp + fn)\n",
"recall = recall_score(labels,predicted)\n",
"print('Recall: %f' % recall)\n",
"\n",
"# f1: 2 tp / (2 tp + fp + fn)\n",
"f1 = f1_score(labels,predicted)\n",
"print('F1 score: %f' % f1)\n",
"\n",
"confusion_matrix(labels,predicted)"
]
}
],
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Xet Storage Details

Size:
4.13 kB
·
Xet hash:
ff1fbf20e8a73293a8f835e83d1193dc9383aa298b5f34e00072c84de95788a3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.