How to store directories of image as target name and image array to DataFrame pandas

Viewed 76

I have an image directories like this:

enter image description here

and I have a code like this following:

base_dir = 'dataset/'
data = []
target = []
for folder in os.listdir(base_dir):
  files = os.listdir(os.path.join(base_dir, folder))
  for batch in files:
      
    img = cv2.imread(os.path.join(base_dir, folder, batch))
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)

    canny = cv2.Canny(thresh,50,100)
  
    circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10000,param1=50,param2=30,minRadius=0,maxRadius=1000)
    height,width = gray.shape
    r = 0
    mask = np.zeros((height,width), np.uint8)
    for i in circles[0,:]:
        cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),3)
        cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1)
        blank_image = img[:int(i[1]),:int(i[1])]

        masked_data = cv2.bitwise_and(gray, gray, mask=mask)
        _,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)
        contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        x,y,w,h = cv2.boundingRect(contours[0][0])
        crop = masked_data[y:y+h,x:x+w]
        r = i[2]
        crop_dim =cv2.cvtColor(crop, cv2.COLOR_RGB2BGR)

    # liang's code for reference
    def daugman_normalization(image, height, width, r_in, r_out):       
        thetas = np.arange(0, 2 * np.pi, 2 * np.pi / width)  # Theta values
        r_out = r_in + r_out
        # Create empty flatten image
        flat = np.zeros((height,width, 3), np.uint8)
        circle_x = int(image.shape[0] / 2)
        circle_y = int(image.shape[1] / 2)

        for i in range(width):
            for j in range(height):
                theta = thetas[i]  # value of theta coordinate
                r_pro = j / height  # value of r coordinate(normalized)

                # get coordinate of boundaries
                Xi = circle_x + r_in * np.cos(theta)
                Yi = circle_y + r_in * np.sin(theta)
                Xo = circle_x + r_out * np.cos(theta)
                Yo = circle_y + r_out * np.sin(theta)

                # the matched cartesian coordinates for the polar coordinates
                Xc = (1 - r_pro) * Xi + r_pro * Xo
                Yc = (1 - r_pro) * Yi + r_pro * Yo

                color = image[int(Xc)][int(Yc)]  # color of the pixel

                flat[j][i] = color
        return flat  
    try:
      image_nor = daugman_normalization(crop_dim,30,180,50,50)
    except:
      image_nor = daugman_normalization(crop_dim,30,180,45,45)

    image_nor = image_nor[:,:,0]
    image_nor = np.asarray(image_nor)
    image_nor = image_nor.flatten()

    data.append(image_nor)
    nama_target = os.path.splitext(folder)
    target.append(nama_target)

  all_imgs = []
  for img in data: 
      all_imgs.append(img)
  all_imgs.append(target)# all_imgs.append(label)
                                
columns = []
x = range(1,5400)
for n in x:
  columns.append(n)
columns.append("target")
df = pd.DataFrame(all_imgs,columns = columns)
df

and the dataframe output like this:

enter image description here

but, I wanna make a "target" column with the folder name (directories of image), and the image have 5400 pixel with total all column 5401 (5400 for the value of image's pixel and 1 another column for target name)

and my expect DataFrame output with the following image:

enter image description here

How can I change the code with the output like this?

Thank you for advance!

0 Answers
Related