在Cloud Shell上运行Python脚本并将ouput保存到Cloud SQL中



我想运行一个python脚本,该脚本需要大约350个图像的输入,并且生成的输出有超过7亿行,因此需要花费大量时间来处理。这就是为什么我想在谷歌云外壳上运行脚本并以云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()

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

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

相关内容

  • 没有找到相关文章

最新更新