[PYTHON] Label images on jupyter lab

Thing you want to do

I want to label the images I want to use for AI, but it is troublesome to create a GUI. .. .. I thought I wondered if it could be done on JupyterLab, and I did a lot of research, so I made a note at that time. An image like inputting labeling with input while updating the image displayed with matplot.

Execution environment

code

Label the images in the sample_img folder.

Preparation

import cv2
import matplotlib.pyplot as plt
import os
import glob
import IPython
#Get the path of the image
img_list = glob.glob(os.path.join(r"sample_img","*.jpg "))
img_list
>>> ['sample_img/img3.jpg', 'sample_img/img2.jpg', 'sample_img/img1.jpg']

Code to display the image

name_list = []
for img_path in img_list:
    #Loading images
    img = cv2.imread(img_path)
    #Frame to display image
    fig = plt.figure(figsize=(5,5))
    ax = fig.add_subplot(1,1,1)
    ax.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
    plt.title(img_path)
    plt.pause(.01)
    
    #Text box for labeling
    comment = input()
   
    if comment == "break":
        break
    else:
        #Clear the displayed image
        IPython.display.clear_output()
        #Add the string entered in Input to the list
        name_list.append(comment)

The operation screen looks like this. passlabeling.gif

Verification

# name_Check list
name_list
>>> ['dog', 'cat', 'cat']

Finally

I wish I could make the Input part look like a radio button.

Recommended Posts

Label images on jupyter lab
Display PIL images on Jupyter
Jupyter Lab begins
Jupyter on AWS
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Hello X3DOM on Jupyter
Install Python Jupyter lab
Install octave_kernel on Jupyter [additional]
Run Jupyter on Ubuntu on Windows
Building a Jupyter Lab development environment on WSL2 using Anaconda3
Building a Python environment on a Mac and using Jupyter lab
Smoothly reload modules on jupyter
Make Jupyter Lab accessible remotely
High charts on Jupyter notebook
View PDF on Jupyter Notebook
Error: 500 (Internal Server Error) Jupyter lab
Run Jupyter Notebook on windows
I tried to visualize BigQuery data using Jupyter Lab on GCP
Formatting with autopep8 on Jupyter notebook
Run Open Modelica from Jupyter Lab
Run azure ML on jupyter notebook
Try running Jupyter Notebook on Mac
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)