'''
Python webtest by 郑瑞国
教学用web网站,当前目录下放置html网页、png、jpeg图片、
mp4视频、PDF、doc文档,可在客户端浏览器通过网页展示。
'''
from bottle import route,run,get,post,request,static_file,template,error
import threading
import socket
import random
import os
@route('/hello/:name')
def index(name='World'):
return '<h1>Hello %s!</h1>'%name
@route('/hello')
def hello():
print('/hello')
return template('hello_template.htm',name='郑瑞国') #'<h1>Hello World!</h1>'
@route('/')
def greet(name='Dir'):
dirList = os.listdir('.')
dirStr = '<p>'.join(dirList)
print(dirStr)
return 'Hello %s,how are you?<p>'%name+str(dirStr)
@get('/login')#或@route('/login')
def login_form():
print('/login')
return'''<form method="POST"action="/login">
<input name="name"type="text"/>
<input name="password"type="password"/>
<input type="submit"/>
</form>'''
@post('/login')#或@route('/login', method='POST')
def login_submit():
name=request.forms.get('name')
password=request.forms.get('password')
if name == password: #check_login(name, password):
return"<p>登录成功</p>"
else:
return"<p>登录失败</p>"
@route('/image/<filename:re:.*\.png>')
def send_image(filename='desert.png'):
return static_file(filename,root=r'./',mimetype='image/png')
@route('/image/<filename:re:.*\.jpg>')
def send_image(filename='desert.jpg'):
return static_file(filename,root=r'./',mimetype='image/jpg')
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath,root=r'./')
@get('/upfile')#或@route('/uppic')
def uppic_form():
print('/upfile')
return '''<form action = "/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category"/>
Select a file: <input type="file" name="upload"/>
<input type="submit" value="Start upload"/>
</form> '''
@route('/upload',method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name,ext = os.path.splitext(upload.filename)
print(category,upload,name,ext)
if ext not in ('.png','.jpg','.jpeg'):
return 'File extension not allowed.'
save_path = ('./')
upload.save(save_path)
return 'OK'
@route('/download/<filename:path>')
def download(filename):
return static_file(filename,root='./',download=filename)
@route('/get_ip')
def show_ip():
ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
return template("Server IP is:{{ip}}",ip=ip)
@route('/show_text/:text')
def show_text(text='hello'):
ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
return template("Server IP is:{{ip}}",ip=ip+text)
def videoDetect(filename):
ip = ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
os.chdir(r'd:/AI-OK/new/')
os.system('python VideoDetection.py %s'%filename)
return template("Your IP is:{{ip}}",ip=ip)
@route('/start/<filename:path>')
def start(filename):
ip = ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
threading._start_new_thread(videoDetect,(filename,))
return template("<p>Your IP is:{{ip}}</p> <p>Your filename is:{{filename}}</p>",ip=ip,filename=filename)
@route('/detect/<filename:path>')
def detect(filename):
ip = ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
threading._start_new_thread(videoDetect,(filename,))
return template("<p>Your IP is:{{ip}}</p> <p>Your filename is:{{filename}}</p>",ip=ip,filename=filename)
@route('/stop')
def stop():
ip = ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
return template("Your IP is:{{ip}}",ip=ip)
@route('/get_file')
def get_file():
ip = ip = socket.gethostbyname(socket.gethostname()) #request.environ.get('REMOTE_ADDR')
return template("Your IP is:{{ip}}",ip=ip)
@route('/get_message')
def get_message():
a = random.randint(80,98)
b = random.randint(80,98)
c = random.randint(80,98)
d = random.randint(80,98)
e = random.randint(80,98)
f = random.randint(80,98)
g = random.randint(80,98)
h = random.randint(80,98)
i = random.randint(80,98)
j = random.randint(80,98)
message = '[{"课堂巡视":'+str(a)+',"提问与互动":'+str(b)+',"个别指导":'+str(c)+',"板书规范":'+str(d)+',"教授内容":'+str(e)+',"实训规范操作":'+str(f)+',"课程资源运用":'+str(g)+',"肢体语言运用":'+str(h)+',"仪容仪表":'+str(i)+',"出勤":'+str(j)+'}]'
return template("message:{{message}}",message=message)
#@error(404)
def error404(error):
return 'Nothong her,sorry'
run(host='0.0.0.0',port=8000,server='paste')
from imageai.Detection import VideoObjectDetection
from imageai.Detection import ObjectDetection
import os
import cv2
execution_path = os.getcwd()
video_file_path = './20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4'
detected_file_path = video_file_path.strip('.mp4')+'_camera_detected_video'
imdetector = ObjectDetection()
imdetector.setModelTypeAsYOLOv3()
imdetector.setModelPath( os.path.join(execution_path , "yolo.h5"))
imdetector.loadModel()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
#detector.loadModel(detection_speed = 'flash')
#video="http://admin:admin@192.168.1.105:8081/" #此处@后的ipv4 地址需要修改
#camera = cv2.VideoCapture(video) #可使用参数video访问网络视频
#cap = cv2.VideoCapture('./20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4')
#def forFrame(frame_number,
# output_array,
# output_count,
# returned_frame
# ):
def forFrame(frame_number, output_array, output_count, returned_frame):
global video_file_path
global detected_file_path
#cv2.imshow('camera',returned_frame)
#cv2.waitKey(1)
#print('frame_number:',frame_number)
#print('output_array:',output_array)
#print('output_count:',output_count)
try:
if output_count['person']>=1:
print(output_count['person'])
print('output_array:',output_array)
with open('./video-detected/'+detected_file_path+'_output_array_log.txt','at') as f:
f.write(str(output_array)+'\n')
for eachObject in output_array:
print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
print("--------------------------------")
print('output_count:',output_count)
with open('./video-detected/'+detected_file_path+'_output_count_log.txt','at') as f:
f.write(str(output_count)+'\n')
if output_count['person']>=1:
#print('1')
impath='img'+str(frame_number)+'.jpg'
cv2.imwrite('./img/'+impath,returned_frame)
imdetections = imdetector.detectObjectsFromImage(
input_image=os.path.join(execution_path,'img/'+impath),
output_image_path=os.path.join(execution_path,'img/'+'new'+impath))
#print(2)
print(imdetections)
for eachObject in imdetections:
print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
print("--------------------------------")
cv2.putText(returned_frame, 'Focus:'+str(eachObject['percentage_probability']), eachObject['box_points'][:2], cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
cv2.putText(returned_frame, 'Focus', (40,50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
#print(3)
cv2.imshow('camera',returned_frame)
cv2.waitKey(1)
except:
pass
def videoDtection(input_file_path=video_file_path):
global video_file_path
global detected_file_path
video_file_path = input_file_path
detected_file_path = video_file_path.strip('.mp4')+'_camera_detected_video'
video_detected_path = detector.detectObjectsFromVideo(
input_file_path='./video/'+input_file_path, #'./20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4',#camera_input=cap,
output_file_path='./video-detected/'+detected_file_path, #os.path.join(execution_path, "camera_detected_video"),
frames_per_second=20,
per_frame_function=forFrame,
log_progress=False,
minimum_percentage_probability=30,
return_detected_frame=True,
frame_detection_interval=5,
display_percentage_probability=False,
display_object_name=False)
return video_detected_path #'./video-detected'+detected_file_path
if __name__ == "__main__":
input_file = os.sys.argv[1]
videoDtection(input_file)
# websocketdemo.py
from bottle import route, run, static_file,template
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket
import threading
import time
from imageai.Detection import VideoObjectDetection
#from imageai.Detection import ObjectDetection
import os
import cv2
execution_path = os.getcwd()
#cap = cv2.VideoCapture('./20190911_13_04_20_101601_admin_4_5_film.mp4')
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
#detector.loadModel(detection_speed = 'flash')
@route("/")
def callback():
return static_file("websocket.html", root=".")
@route("/websocket", apply=[websocket])
def callback(ws):
#count = 0
#ws.send(str(count)+'Hello World'+'课堂巡视 ')
# count = 0
# while True:
# count += 1
# time.sleep(1)
# ws.send(str(count)+'Hello World'+'课堂巡视 ')
def forFrame(frame_number, output_array, output_count, returned_frame):
#cv2.imshow('camera',returned_frame)
#cv2.waitKey(1)
print('frame_number:',str(frame_number))
print('output_array:',str(output_array))
print('output_count:',str(output_count))
ws.send('Hello')
ws.send(str(frame_number))
#ws.send('output_array:',str(output_array))
#ws.send('output_count:',str(output_count))
video_path = detector.detectObjectsFromVideo(
input_file_path='./20190911_13_04_20_101601_admin_4_5_film.mp4',#camera_input=cap,
output_file_path=os.path.join(execution_path, "camera_detected_video"),
frames_per_second=20,
per_frame_function=forFrame,
log_progress=False,
minimum_percentage_probability=30,
return_detected_frame=True,
frame_detection_interval=5,
display_percentage_probability=False,
display_object_name=False)
def VideoDetector(ws):
print('abcdefg')
ws.send('abcdefg')
# def forFrame(frame_number, output_array, output_count, returned_frame):
# #cv2.imshow('camera',returned_frame)
# #cv2.waitKey(1)
# print('frame_number:',str(frame_number))
# print('output_array:',str(output_array))
# print('output_count:',str(output_count))
# #ws.send('frame_number:',frame_number)
# #ws.send('output_array:',output_array)
# #ws.send('output_count:',output_count)
#
#
# video_path = detector.detectObjectsFromVideo(
# input_file_path='./20190911_13_04_20_101601_admin_4_5_film.mp4',#camera_input=cap,
# output_file_path=os.path.join(execution_path, "camera_detected_video"),
# frames_per_second=20,
# per_frame_function=forFrame,
# log_progress=False,
# minimum_percentage_probability=30,
# return_detected_frame=True,
# frame_detection_interval=5,
# display_percentage_probability=False,
# display_object_name=False)
#@route("/websocket", apply=[websocket])
def detect(ws):
threading._start_new_thread(VideoDetector,(ws,))
return template("<p>websocket starting</p>")
run(host='0.0.0.0', port=10000, server=GeventWebSocketServer)
from imageai.Detection import VideoObjectDetection
#from imageai.Detection import ObjectDetection
import os
import cv2
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
#video="http://admin:admin@192.168.1.105:8081/" #此处@后的ipv4 地址需要修改
#camera = cv2.VideoCapture(video) #可使用参数video访问网络视频
#cap = cv2.VideoCapture('./20190911_13_04_20_101601_admin_4_5_film.mp4')
#def forFrame(frame_number,
# output_array,
# output_count,
# returned_frame
# ):
def forFrame(frame_number, output_array, output_count, returned_frame):
cv2.imshow('camera',returned_frame)
cv2.waitKey(1)
print('frame_number:',frame_number)
print('output_array:',output_array)
print('output_count:',output_count)
# try:
# if output_count['person']>=1:
# print(output_count['person'])
# print('output_array:',output_array)
# for eachObject in output_array:
# print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
# print("--------------------------------")
# print('output_count:',output_count)
#
# if output_count['person']>=1:
# #print('1')
# impath='img'+str(frame_number)+'.jpg'
# cv2.imwrite('./img/'+impath,returned_frame)
# imdetections = imdetector.detectObjectsFromImage(
# input_image=os.path.join(execution_path,'img/'+impath),
# output_image_path=os.path.join(execution_path,'img/'+'new'+impath))
# #print(2)
# print(imdetections)
# for eachObject in imdetections:
# print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
# print("--------------------------------")
# cv2.putText(returned_frame, 'Focus:'+str(eachObject['percentage_probability']), eachObject['box_points'][:2], cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
# cv2.putText(returned_frame, 'Focus Happy', (40,50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
# #print(3)
# cv2.imshow('camera',returned_frame)
# cv2.waitKey(1)
#
# except:
# pass
#
#imdetector = ObjectDetection()
#imdetector.setModelTypeAsYOLOv3()
#imdetector.setModelPath( os.path.join(execution_path , "yolo.h5"))
#imdetector.loadModel()
#detector.loadModel(detection_speed = 'flash')
video_path = detector.detectObjectsFromVideo(
input_file_path='./20190911_13_04_20_101601_admin_4_5_film.mp4',#camera_input=cap,
output_file_path=os.path.join(execution_path, "camera_detected_video"),
frames_per_second=20,
per_frame_function=forFrame,
log_progress=False,
minimum_percentage_probability=30,
return_detected_frame=True,
frame_detection_interval=5,
display_percentage_probability=False,
display_object_name=False)
<!DOCTYPE html>
<html>
<head>
<title>websocket demo</title>
<script>
var ws = new WebSocket('ws:localhost:10000/websocket');
ws.onopen = function(evt){
console.log("on open");
}
ws.onclose = function(evt){
console.log("on close");
}
ws.onmessage = function(evt){
console.log(evt.data);
}
</script>
</head>
<body>
</body
</html>