例:下载、上传文件
# coding: utf-8from ftplib import FTP
import time
import tarfile
import os
# !/usr/bin/python
# -*- coding: utf-8 -*-
from ftplib import FTP
def ftpconnect(host, username, password):
ftp = FTP()
# ftp.set_debuglevel(2)
ftp.connect(host, 21)
ftp.login(username, password)
return ftp
#从ftp下载文件
def downloadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'wb')
ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize)
ftp.set_debuglevel(0)
fp.close()
#从本地上传文件到ftp
def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize)
ftp.set_debuglevel(0)
fp.close()
if __name__ == "__main__":
ftp = ftpconnect("113.105.139.xxx", "ftp***", "Guest***")
downloadfile(ftp, "Faint.mp4", "C:/Users/Administrator/Desktop/test.mp4")
#调用本地播放器播放下载的视频
os.system('start "C:\Program Files\Windows Media Player\wmplayer.exe" "C:/Users/Administrator/Desktop/test.mp4"')
uploadfile(ftp, "C:/Users/Administrator/Desktop/test.mp4", "test.mp4")
ftp.quit()
批量获取图片:
keras 多张图片:
很多情况下,你并不能使用以上这些方法来直接输入数据去训练或者预测,原因是你的数据集太大了,没办法把所有的图片都载入到内存当中。那keras的data generator就派上用场了,当你的模型需要训练数据的时候,generator会自动从cpu生成一批图片,喂到GPU里面让模型进行训练,依次循环,直到训练结束。
压缩数据中维度为1的维度, numpy.squeeze()
模型是不能直接对图片进行卷积操作的,必须先转化为numpy数组才能输入模型里面去,而且如果数据集的图片尺寸不统一,也有不同的操作细节。
keras 模型保存路径: C:\Users\你的用户名.keras\models
notop代表是否包括顶层的全连接层,默认include_top=True,包括全连接层。
tf -- tensorflow 或者 CNTK
th -- theano
# coding:utf8import urllib.request
import os
def download_img(img_url):
request = urllib.request.Request(img_url)
try:
response = urllib.request.urlopen(request)
img_name = img_url.split('/')[-1]
print(img_name)
filename = os.getcwd() + '/' + img_name
print(filename)
code = response.getcode()
print(code)
if (code == 200):
with open(filename, 'wb') as f:
f.write(response.read()) # 将内容写入图片
return filename
except:
return "failed"
if __name__ == '__main__':
# 下载要的图片
img_url = ''
print (download_img(img_url)+' download successfully!')