0

I want to run a python script which takes input around 350 images and the output which is generated has more than 700 million rows so it takes a lot of time to process. That's why i wanted to run the script on google cloud shell and save the output in the form of cloud sql database. I want to know how i can modify and run the script on shell so that it takes input from bucket and save the output into database. Any help is appreciated

import cv2 
import numpy as np
import sqlite3
from skimage import data
from skimage import filters
from skimage import exposure
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
import glob
import csv

ksize=31
filters1=[]
def build_filters():
    for theta in np.arange(0, (np.pi* 0.75), np.pi/4):
        for lamda in np.arange(0, np.pi, np.pi/8):
            kernel= cv2.getGaborKernel((ksize, ksize), 0.8, theta, lamda, 0.8, 0, cv2.CV_32F )
            kernel= kernel / (1.5* kernel.sum())
            filters1.append(kernel)
    return filters1

def process_img(img, filters):

    accum=np.zeros_like(img)
    for kernel in filters1:
        filtered= cv2.filter2D(img, cv2.CV_8UC3, kernel)
        np.maximum(accum, filtered, accum)
    return accum

filters1=build_filters()

conn = sqlite3.connect('train.db')
c = conn.cursor()

def create_table():
     c.execute("CREATE TABLE IF NOT EXISTS features(intensity REAL,red REAL,green REAL,blue REAL,yellow REAL,Orientation REAL,foreground REAL)")
create_table()

def dynamic_data_entry(I,R,G,B,Y,O,fore):
    intensity       = float(I)
    red             = float(R)
    green           = float(G)
    blue            = float(B)
    yellow          = float(Y)
    Orientation     = float(O)
    foreground      = int(fore)

    c.execute("INSERT INTO features (intensity,red,green,blue,yellow,Orientation,foreground) VALUES(?, ?, ?, ?, ?, ?, ?)",
                                (intensity,red,green,blue,yellow,Orientation,foreground))

    conn.commit()

for file in  glob.glob('c:/Users/shafee/Downloads/image dataset/1080p/*.jpg'):
           img = cv2.imread(file)
           img = cv2.resize(img,(192,108))
           r=img[:,:,2]
           g=img[:,:,1]
           b=img[:,:,0]
           img1=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
           val=filters.threshold_otsu(img1)
           foreground=img1<val

           R=r- (g + b)/2
           I=(r + g + b)/3
           G= g - ( r + b)/2
           B= b - (r + g)/2
           Y =(r + g)/2 -abs(r - g)/2 - b
           O= process_img(img1, filters1)
           I1=I.reshape(-1,1); I1 = np.divide(I1,255)
           R1=R.reshape(-1,1); R1 = np.divide(R1,255)
           G1=G.reshape(-1,1); G1 = np.divide(G1,255)
           B1=B.reshape(-1,1); B1 = np.divide(B1,255)
           Y1=Y.reshape(-1,1); Y1 = np.divide(Y1,255)
           O1=O.reshape(-1,1); O1 = np.divide(O1,255)
           fore1=foreground.reshape(-1,1)

           for i in range(20736):
               dynamic_data_entry(I1[i], R1[i], G1[i], B1[i], Y1[i], O1[i], int(fore1[i]==True))

    c.close
    conn.close()

1 Answers1

1

First, you will need to use Cloud Storage Client Library for Python. To read from an existing bucket, you'll need to set up authentication and then you can use the client library. Example code is provided in the link above.

The second step is Connecting to Cloud SQL from external application (like your script) to write data to your database. The code samples are also provided here.

arudzinska
  • 3,152
  • 1
  • 16
  • 29