在云外壳上运行python脚本并将输出保存到云sql中

时间:2018-06-02 20:03:20

标签: python virtual-machine google-cloud-sql google-cloud-shell

我想运行一个python脚本,它输入大约350个图像,生成的输出有超过7亿行,因此需要花费大量的时间来处理。这就是为什么我想在google cloud shell上运行脚本并以云sql数据库的形式保存输出的原因。我想知道如何在shell上修改和运行脚本,以便从存储桶中获取输入并将输出保存到数据库中。任何帮助表示赞赏

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 个答案:

答案 0 :(得分:0)

首先,您需要为Python使用cumsum。要从现有存储桶中读取数据,您需要设置身份验证,然后才能使用客户端库。上面的链接中提供了示例代码。

第二步是Cloud Storage Client Library(如您的脚本)将数据写入数据库。这里也提供了代码示例。