332 lines
12 KiB
Python
332 lines
12 KiB
Python
import os,torch,cv2,time
|
||
from System_I import Detection_Y5,Fine_grained
|
||
from vediotoima import vedio_img
|
||
from common import global_para as gp
|
||
from video_notice import VideoNotice
|
||
#!/usr/bin/python
|
||
# -*- coding: UTF-8 -*-
|
||
|
||
from ftplib import FTP, error_perm
|
||
import socket
|
||
import sys
|
||
import time
|
||
import os
|
||
def GYD_RedCameraDeal (root,DetectionCalss,Fine_grainedClass):
|
||
CameraLists = os.listdir(root)
|
||
print(CameraLists)
|
||
for CameraIMEI in CameraLists:
|
||
if not os.path.exists(os.path.join(root+"_Result",CameraIMEI)):
|
||
os.makedirs(os.path.join(root+"_Result",CameraIMEI))
|
||
image_videolists = os.listdir(os.path.join(root,CameraIMEI))
|
||
for image_video in image_videolists:
|
||
print(image_videolists)
|
||
if "JPG" in image_video:
|
||
pass
|
||
# print(image_video, " is starting")
|
||
# image = cv2.imread(os.path.join(root,CameraIMEI,image_video))
|
||
# DetectionCalss.inference_Red(image,os.path.join(root,CameraIMEI,image_video),Fine_grainedClass)
|
||
# print(image_video," is ok")
|
||
else:
|
||
video_path = os.path.join(root, CameraIMEI,image_video)
|
||
video_path_out = os.path.join(root + "_Result", CameraIMEI, "RAWImage",image_video.replace(".MP4",""))
|
||
vedio_img(video_path,video_path_out,25)
|
||
isExists = os.path.exists(os.path.join(root + "_Result", CameraIMEI, "VideoResult",image_video.replace(".MP4","")))
|
||
if not isExists:
|
||
os.makedirs(os.path.join(root + "_Result", CameraIMEI, "VideoResult",image_video.replace(".MP4","")))
|
||
img_lists = os.listdir(video_path_out)
|
||
img_lists.sort()
|
||
|
||
for img in img_lists:
|
||
|
||
image = cv2.imread(os.path.join(video_path_out, img))
|
||
DetectionCalss.inference_Red(image, os.path.join(root, CameraIMEI, "VideoResult",image_video.replace(".MP4",""),img),
|
||
Fine_grainedClass)
|
||
|
||
|
||
|
||
|
||
|
||
|
||
#if __name__ == '__main__':
|
||
|
||
|
||
|
||
|
||
|
||
class MyFTP:
|
||
def __init__(self, host, port):
|
||
""" 初始化 FTP 客户端
|
||
参数:
|
||
host:ip地址
|
||
|
||
port:端口号
|
||
"""
|
||
# print("__init__()---> host = %s ,port = %s" % (host, port))
|
||
|
||
self.host = host
|
||
self.port = port
|
||
self.ftp = FTP()
|
||
# 重新设置下编码方式
|
||
self.ftp.encoding = 'gbk'
|
||
#self.log_file = open("/home/gyd/ftp/log/log.txt", "a")
|
||
self.file_list = []
|
||
|
||
|
||
def login(self, username, password):
|
||
""" 初始化 FTP 客户端
|
||
参数:
|
||
username: 用户名
|
||
|
||
password: 密码
|
||
"""
|
||
try:
|
||
timeout = 1000
|
||
socket.setdefaulttimeout(timeout)
|
||
# 0主动模式 1 #被动模式
|
||
self.ftp.set_pasv(True)
|
||
# 打开调试级别2,显示详细信息
|
||
# self.ftp.set_debuglevel(2)
|
||
|
||
self.debug_print('开始尝试连接到 %s' % self.host)
|
||
self.ftp.connect(self.host, self.port)
|
||
self.debug_print('成功连接到 %s' % self.host)
|
||
|
||
self.debug_print('开始尝试登录到 %s' % self.host)
|
||
self.ftp.login(username, password)
|
||
self.debug_print('成功登录到 %s' % self.host)
|
||
|
||
self.debug_print(self.ftp.welcome)
|
||
except Exception as err:
|
||
self.debug_print("FTP 连接或登录失败 ,错误描述为:%s" % err)
|
||
pass
|
||
|
||
def close(self):
|
||
""" 退出ftp
|
||
"""
|
||
self.debug_print("close()---> FTP退出")
|
||
self.ftp.quit()
|
||
|
||
def get_file_list(self, line):
|
||
""" 获取文件列表
|
||
参数:
|
||
line:
|
||
"""
|
||
file_arr = self.get_file_name(line)
|
||
# 去除 . 和 ..
|
||
if file_arr[1] not in ['.', '..']:
|
||
self.file_list.append(file_arr)
|
||
|
||
def get_file_name(self, line):
|
||
""" 获取文件名
|
||
参数:
|
||
line:
|
||
"""
|
||
pos = line.rfind(':')
|
||
while (line[pos] != ' '):
|
||
pos += 1
|
||
while (line[pos] == ' '):
|
||
pos += 1
|
||
file_arr = [line[0], line[pos:]]
|
||
return file_arr
|
||
|
||
def download_file(self, local_file, remote_file):
|
||
"""从ftp下载文件
|
||
参数:
|
||
local_file: 本地文件
|
||
|
||
remote_file: 远程文件
|
||
"""
|
||
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
|
||
|
||
if self.is_same_size(local_file, remote_file):
|
||
self.debug_print('%s 文件大小相同,无需下载' % local_file)
|
||
return
|
||
else:
|
||
try:
|
||
self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
|
||
buf_size = 1024
|
||
file_handler = open(local_file, 'wb')
|
||
self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
|
||
file_handler.close()
|
||
except Exception as err:
|
||
self.debug_print('下载文件出错,出现异常:%s ' % err)
|
||
return
|
||
|
||
def download_file_tree(self, local_path, remote_path):
|
||
"""从远程目录下载多个文件到本地目录
|
||
参数:
|
||
local_path: 本地路径
|
||
|
||
remote_path: 远程路径
|
||
"""
|
||
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
|
||
try:
|
||
self.ftp.cwd(remote_path)
|
||
except Exception as err:
|
||
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
|
||
return
|
||
|
||
if not os.path.isdir(local_path):
|
||
self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
|
||
os.makedirs(local_path)
|
||
|
||
self.debug_print('切换至目录: %s' % self.ftp.pwd())
|
||
|
||
self.file_list = []
|
||
# 方法回调
|
||
self.ftp.dir(self.get_file_list)
|
||
|
||
remote_names = self.file_list
|
||
self.debug_print('远程目录 列表: %s' % remote_names)
|
||
for item in remote_names:
|
||
file_type = item[0]
|
||
file_name = item[1]
|
||
local = os.path.join(local_path, file_name)
|
||
if file_type == 'd':
|
||
print("download_file_tree()---> 下载目录: %s" % file_name)
|
||
self.download_file_tree(local, file_name)
|
||
self.ftp.cwd("..")
|
||
self.debug_print('返回上层目录 %s' % self.ftp.pwd())
|
||
elif file_type == '-':
|
||
print("download_file()---> 下载文件: %s" % file_name)
|
||
self.download_file(local, file_name)
|
||
"""
|
||
"""
|
||
return True
|
||
|
||
def upload_file(self, local_file, remote_file):
|
||
"""从本地上传文件到ftp
|
||
|
||
参数:
|
||
local_path: 本地文件
|
||
|
||
remote_path: 远程文件
|
||
"""
|
||
if not os.path.isfile(local_file):
|
||
self.debug_print('%s 不存在' % local_file)
|
||
return
|
||
|
||
if self.is_same_size(local_file, remote_file):
|
||
self.debug_print('跳过相等的文件: %s' % local_file)
|
||
return
|
||
|
||
buf_size = 1024
|
||
file_handler = open(local_file, 'rb')
|
||
self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
|
||
file_handler.close()
|
||
self.debug_print('上传: %s' % local_file + "成功!")
|
||
|
||
def upload_file_tree(self, local_path, remote_path):
|
||
"""从本地上传目录下多个文件到ftp
|
||
参数:
|
||
|
||
local_path: 本地路径
|
||
|
||
remote_path: 远程路径
|
||
"""
|
||
if not os.path.isdir(local_path):
|
||
self.debug_print('本地目录 %s 不存在' % local_path)
|
||
return
|
||
|
||
try:
|
||
self.ftp.cwd(remote_path)
|
||
self.debug_print('切换至远程目录: %s' % self.ftp.pwd())
|
||
except error_perm:
|
||
self.debug_print('远程目录%s不存在,先创建远程目录' % remote_path)
|
||
self.ftp.mkd(remote_path)
|
||
self.ftp.cwd(remote_path)
|
||
self.debug_print('切换至远程目录: %s' % self.ftp.pwd())
|
||
|
||
local_name_list = os.listdir(local_path)
|
||
for local_name in local_name_list:
|
||
src = os.path.join(local_path, local_name)
|
||
if os.path.isdir(src):
|
||
try:
|
||
self.ftp.mkd(local_name)
|
||
except Exception as err:
|
||
self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
|
||
self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
|
||
self.upload_file_tree(src, local_name)
|
||
else:
|
||
self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
|
||
self.upload_file(src, local_name)
|
||
self.ftp.cwd("..")
|
||
|
||
def debug_print(self, s):
|
||
""" 打印日志
|
||
"""
|
||
self.write_log(s)
|
||
|
||
def write_log(self, log_str):
|
||
""" 记录日志
|
||
参数:
|
||
log_str:日志
|
||
"""
|
||
time_now = time.localtime()
|
||
date_now = time.strftime('%Y-%m-%d %H:%M:%S', time_now)
|
||
format_log_str = "%s ---> %s \n " % (date_now, log_str)
|
||
print(format_log_str)
|
||
#self.log_file.write(format_log_str)
|
||
|
||
def deal_error(self, e):
|
||
""" 处理错误异常
|
||
参数:
|
||
e:异常
|
||
"""
|
||
log_str = '发生错误: %s' % e
|
||
self.write_log(log_str)
|
||
sys.exit()
|
||
|
||
def is_same_size(self, local_file, remote_file):
|
||
"""判断远程文件和本地文件大小是否一致
|
||
|
||
参数:
|
||
local_file: 本地文件
|
||
|
||
remote_file: 远程文件
|
||
"""
|
||
try:
|
||
remote_file_size = self.ftp.size(remote_file)
|
||
except Exception as err:
|
||
# self.debug_print("is_same_size() 错误描述为:%s" % err)
|
||
remote_file_size = -1
|
||
|
||
try:
|
||
local_file_size = os.path.getsize(local_file)
|
||
except Exception as err:
|
||
# self.debug_print("is_same_size() 错误描述为:%s" % err)
|
||
local_file_size = -1
|
||
|
||
self.debug_print('local_file_size:%d , remote_file_size:%d' % (local_file_size, remote_file_size))
|
||
if remote_file_size == local_file_size:
|
||
return 1
|
||
else:
|
||
return 0
|
||
|
||
|
||
def Hatcher_R():
|
||
print("start system")
|
||
data_today = time.strftime('%Y%m%d',time.localtime(time.time()))
|
||
my_ftp = MyFTP("113.204.49.10", 802)
|
||
my_ftp.login("camera", "Birdsdata2020")
|
||
my_ftp.download_file_tree(os.path.join("/home/gyd/Infrared",data_today,"ftp","860548046389083"),"/home/camera/860548046389083/")
|
||
print("860548046389083 down")
|
||
my_ftp.download_file_tree(os.path.join("/home/gyd/Infrared", data_today,"ftp", "860548046386865"), "/home/camera/860548046386865/")
|
||
print("860548046386865 down")
|
||
my_ftp.download_file_tree(os.path.join("/home/gyd/Infrared", data_today,"ftp", "860548042913449"), "/home/camera/860548042913449/")
|
||
print("860548042913449 down")
|
||
my_ftp.download_file_tree(os.path.join("/home/gyd/Infrared", data_today,"ftp", "860548046390768"), "/home/camera/860548046390768/")
|
||
print("860548046390768 down")
|
||
#my_ftp.upload_file_tree("/data1/changhao/ftp/download/20210422/", "/home/camera/test/")
|
||
my_ftp.close()
|
||
root = os.path.join("/home/gyd/Infrared", data_today,"ftp")
|
||
Detection1 = Detection_Y5(gp.devices, root)
|
||
Fine_grained1 = Fine_grained(gp.devices, root)
|
||
GYD_RedCameraDeal(root,Detection1,Fine_grained1)
|
||
time.sleep(100)
|
||
notice_model = VideoNotice('http://192.168.200.29:1001/guangyd/videoNotice', "Infrared")
|
||
content = notice_model.send_notice()
|
||
print("一批数据处理完毕,已发送信号至服务端来取")
|
||
# import time
|
||
# print(time.strftime('%Y%m%d',time.localtime(time.time())))
|