├── .idea ├── deployment.xml ├── garbage_app_sever.iml ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── workspace.xml ├── app_sever.py ├── motor.py └── start.sh /.idea/deployment.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | -------------------------------------------------------------------------------- /.idea/garbage_app_sever.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 45 | 46 | 59 | 60 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 1614854260691 85 | 129 | 130 | 131 | 132 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /app_sever.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, Response 2 | import cv2, os 3 | import requests 4 | from flask_cors import CORS 5 | from motor import set_servo_open, set_servo_close 6 | import time 7 | 8 | 9 | class VideoCamera(object): 10 | def __init__(self): 11 | # 通过opencv获取实时视频流 12 | self.video = cv2.VideoCapture(0) 13 | 14 | def __del__(self): 15 | self.video.release() 16 | 17 | def get_frame(self): 18 | success, image = self.video.read() 19 | # 因为opencv读取的图片并非jpeg格式,因此要用motion JPEG模式需要先将图片转码成jpg格式图片 20 | ret, jpeg = cv2.imencode('.jpg', image) 21 | return jpeg.tobytes() 22 | 23 | def get_imge(self): 24 | success, image = self.video.read() 25 | return image 26 | 27 | 28 | app = Flask(__name__) 29 | CORS(app, supports_credentials=True) 30 | videoPreview = VideoCamera() 31 | 32 | 33 | def gen(camera): 34 | while True: 35 | frame = camera.get_frame() 36 | # 使用generator函数输出视频流, 每次请求输出的content类型是image/jpeg 37 | yield (b'--frame\r\n' 38 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') 39 | 40 | 41 | @app.route('/video_img_url') # 返回视频流响 42 | def video_feed(): 43 | return Response(gen(videoPreview), 44 | mimetype='multipart/x-mixed-replace; boundary=frame') 45 | 46 | 47 | @app.route('/shut_photho') # 拍照识别内容返回结果 48 | def shut_photho(): 49 | frame = videoPreview.get_imge() 50 | if (os.path.exists("./test.png")): 51 | os.remove("./test.png") 52 | cv2.imwrite("./test.png", frame) 53 | res = upload_image_to_sever_get_result("./test.png") 54 | print(res) 55 | return res 56 | 57 | 58 | @app.route('/open_motor') # 打开舵机 59 | def open_motor(): 60 | channel = int(request.args.get("channel")) 61 | set_servo_open(channel) 62 | time.sleep(5) 63 | set_servo_close(channel) 64 | return "successful" 65 | 66 | 67 | # 上传图片到后台服务器 68 | def upload_image_to_sever_get_result(img_url): 69 | files = {"file": ("img_url" + ".png", open(img_url, 'rb'), "image/png")} 70 | r = requests.post('http://192.168.50.54:5000/predict', files=files) 71 | return r.json()['result'] 72 | 73 | 74 | # host='0.0.0.0',port = 5000 75 | 76 | if __name__ == '__main__': 77 | app.run(host='0.0.0.0', port=8080) 78 | -------------------------------------------------------------------------------- /motor.py: -------------------------------------------------------------------------------- 1 | import time 2 | import Adafruit_PCA9685 3 | 4 | servo_min = 150 # Min pulse length out of 4096 5 | servo_max = 670 # Max pulse length out of 4096 6 | pwm = Adafruit_PCA9685.PCA9685() 7 | pwm.set_pwm_freq(60) 8 | 9 | 10 | def set_servo_open(channel): 11 | time.sleep(2) 12 | pwm.set_pwm(channel, 0, servo_max) 13 | print(channel, "号舵机开启", servo_max) 14 | 15 | 16 | def set_servo_close(channel): 17 | time.sleep(2) 18 | pwm.set_pwm(channel, 0, servo_min) 19 | print(channel, "号舵机关闭", servo_min) 20 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | nohup python3 ./app_sever.py >app_sever_run_log.log 2>&1 & 2 | --------------------------------------------------------------------------------