0

我想运行一个 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()
4

1 回答 1

1

首先,您需要使用Cloud Storage Client Library for Python。要从现有存储桶中读取,您需要设置身份验证,然后您可以使用客户端库。上面的链接中提供了示例代码。

第二步是从外部应用程序(如您的脚本)连接到 Cloud SQL 以将数据写入您的数据库。此处还提供了代码示例。

于 2018-06-25T08:20:11.910 回答