AMMICO/build/html/notebooks/Example objects.ipynb

1153 строки
30 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Objects recognition"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebooks shows how to detect objects quickly using [cvlib](https://github.com/arunponnusamy/cvlib) and the [YOLOv4](https://github.com/AlexeyAB/darknet) model. This library detects faces, people, and several inanimate objects; we currently have restricted the output to person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, cell phone.\n",
"\n",
"The first cell is only run on google colab and installs the [ammico](https://github.com/ssciwr/AMMICO) package.\n",
"\n",
"After that, we can import `ammico` and read in the files given a folder path."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:33:52.229210Z",
"iopub.status.busy": "2023-05-26T09:33:52.228951Z",
"iopub.status.idle": "2023-05-26T09:33:52.238491Z",
"shell.execute_reply": "2023-05-26T09:33:52.237921Z"
}
},
"outputs": [],
"source": [
"# if running on google colab\n",
"# flake8-noqa-cell\n",
"import os\n",
"\n",
"if \"google.colab\" in str(get_ipython()):\n",
" # update python version\n",
" # install setuptools\n",
" # %pip install setuptools==61 -qqq\n",
" # install ammico\n",
" %pip install git+https://github.com/ssciwr/ammico.git -qqq\n",
" # mount google drive for data and API key\n",
" from google.colab import drive\n",
"\n",
" drive.mount(\"/content/drive\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:33:52.241513Z",
"iopub.status.busy": "2023-05-26T09:33:52.241094Z",
"iopub.status.idle": "2023-05-26T09:34:07.302127Z",
"shell.execute_reply": "2023-05-26T09:34:07.301397Z"
}
},
"outputs": [],
"source": [
"import ammico\n",
"from ammico import utils as mutils\n",
"from ammico import display as mdisplay\n",
"import ammico.objects as ob"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Set an image path as input file path."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:07.305858Z",
"iopub.status.busy": "2023-05-26T09:34:07.305086Z",
"iopub.status.idle": "2023-05-26T09:34:07.310201Z",
"shell.execute_reply": "2023-05-26T09:34:07.309601Z"
}
},
"outputs": [],
"source": [
"# Here you need to provide the path to your google drive folder\n",
"# or local folder containing the images\n",
"images = mutils.find_files(\n",
" path=\"data/\",\n",
" limit=10,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:07.313212Z",
"iopub.status.busy": "2023-05-26T09:34:07.312783Z",
"iopub.status.idle": "2023-05-26T09:34:07.315965Z",
"shell.execute_reply": "2023-05-26T09:34:07.315303Z"
}
},
"outputs": [],
"source": [
"mydict = mutils.initialize_dict(images)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Detect objects and directly write to csv\n",
"You can directly carry out the analysis and export the result into a csv. This may take a while depending on how many images you have loaded."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:07.319027Z",
"iopub.status.busy": "2023-05-26T09:34:07.318603Z",
"iopub.status.idle": "2023-05-26T09:34:15.462409Z",
"shell.execute_reply": "2023-05-26T09:34:15.461616Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading yolov4.cfg from https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0% | |\r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading yolov4.weights from https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0% | |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 1% | |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 2% |# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 3% |## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 4% |## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 5% |### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 6% |#### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 7% |##### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 8% |##### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 9% |###### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 10% |####### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 11% |####### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 12% |######## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 13% |######### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 14% |########## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 15% |########## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 16% |########### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 17% |############ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 18% |############ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 19% |############# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 20% |############## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 21% |############### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 22% |############### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 23% |################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 24% |################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 25% |################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 26% |################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 27% |################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 28% |#################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 29% |#################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 30% |##################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 31% |###################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 32% |####################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 33% |####################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 34% |######################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 35% |######################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 36% |######################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 37% |########################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 38% |########################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 39% |############################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 40% |############################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 41% |############################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 42% |############################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 43% |############################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 44% |############################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 45% |################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 46% |################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 47% |################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 48% |################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 49% |################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 50% |#################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 51% |#################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 52% |##################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 53% |###################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 54% |###################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 55% |####################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 56% |######################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 57% |######################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 58% |######################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 59% |########################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 60% |########################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 61% |########################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 62% |############################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 63% |############################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 64% |############################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 65% |############################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 66% |############################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 67% |################################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 68% |################################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 69% |################################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 70% |################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 71% |################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 72% |################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 73% |#################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 74% |##################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 75% |###################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 76% |###################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 77% |####################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 78% |######################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 79% |######################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 80% |######################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 81% |########################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 82% |########################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 83% |########################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 84% |############################################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 85% |############################################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 86% |############################################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 87% |############################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 88% |############################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 89% |################################################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 90% |################################################################ |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 91% |################################################################# |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 92% |################################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 93% |################################################################## |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 94% |################################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 95% |#################################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 96% |##################################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 97% |##################################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 98% |###################################################################### |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 99% |####################################################################### |\r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading yolov3_classes.txt from https://github.com/arunponnusamy/object-detection-opencv/raw/master/yolov3.txt\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100% | |\r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100% | |\r"
]
}
],
"source": [
"for key in mydict:\n",
" mydict[key] = ob.ObjectDetector(mydict[key]).analyse_image()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert the dictionary of dictionarys into a dictionary with lists:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:15.466127Z",
"iopub.status.busy": "2023-05-26T09:34:15.465866Z",
"iopub.status.idle": "2023-05-26T09:34:15.470492Z",
"shell.execute_reply": "2023-05-26T09:34:15.469495Z"
}
},
"outputs": [],
"source": [
"outdict = mutils.append_data_to_dict(mydict)\n",
"df = mutils.dump_df(outdict)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Check the dataframe:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:15.473285Z",
"iopub.status.busy": "2023-05-26T09:34:15.472929Z",
"iopub.status.idle": "2023-05-26T09:34:15.488013Z",
"shell.execute_reply": "2023-05-26T09:34:15.487355Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>filename</th>\n",
" <th>person</th>\n",
" <th>bicycle</th>\n",
" <th>car</th>\n",
" <th>motorcycle</th>\n",
" <th>airplane</th>\n",
" <th>bus</th>\n",
" <th>train</th>\n",
" <th>truck</th>\n",
" <th>boat</th>\n",
" <th>traffic light</th>\n",
" <th>cell phone</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>data/102730_eng.png</td>\n",
" <td>yes</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>yes</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>data/102141_2_eng.png</td>\n",
" <td>yes</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>data/106349S_por.png</td>\n",
" <td>yes</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>no</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" filename person bicycle car motorcycle airplane bus train \n",
"0 data/102730_eng.png yes no no no no no no \\\n",
"1 data/102141_2_eng.png yes no no no no no no \n",
"2 data/106349S_por.png yes no no no no no no \n",
"\n",
" truck boat traffic light cell phone \n",
"0 yes no no no \n",
"1 no no no no \n",
"2 no no no yes "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(10)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Write the csv file:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:15.490872Z",
"iopub.status.busy": "2023-05-26T09:34:15.490647Z",
"iopub.status.idle": "2023-05-26T09:34:15.495564Z",
"shell.execute_reply": "2023-05-26T09:34:15.494952Z"
}
},
"outputs": [],
"source": [
"df.to_csv(\"data_out.csv\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Manually inspect what was detected\n",
"\n",
"To check the analysis, you can inspect the analyzed elements here. Loading the results takes a moment, so please be patient. If you are sure of what you are doing, you can directly export a csv file in the step above.\n",
"Here, we display the object detection results provided by the above library. Click on the tabs to see the results in the right sidebar. You may need to increment the `port` number if you are already running several notebook instances on the same server."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2023-05-26T09:34:15.498370Z",
"iopub.status.busy": "2023-05-26T09:34:15.497940Z",
"iopub.status.idle": "2023-05-26T09:34:15.544173Z",
"shell.execute_reply": "2023-05-26T09:34:15.543446Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dash is running on http://127.0.0.1:8056/\n",
"\n"
]
},
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"100%\"\n",
" height=\"650\"\n",
" src=\"http://127.0.0.1:8056/\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" \n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f287adb7040>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify=\"objects\")\n",
"analysis_explorer.run_server(port=8056)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.16"
},
"vscode": {
"interpreter": {
"hash": "f1142466f556ab37fe2d38e2897a16796906208adb09fea90ba58bdf8a56f0ba"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}