Ubuntu1604下使用web.py创建webservice

本文介绍了一个简单的Python应用,利用web.py框架接收文件路径参数,并返回该路径下所有图片文件的dhash值及其文件地址。该应用适用于处理PNG和JPEG格式的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先安装web.py

sudo pip install web.py

打开一个文件位index.py,输入:

from PIL import Image
import imagehash
import glob
import numpy as np
import json
import web

#URL匹配
urls = (
    '/dhash', 'Dhash'
)

class HashClass(object):
    def __init__(self,dhash,url):
        self.hash=dhash
        self.url=url

class Dhash(object):
    def GET(self):

     address =  web.input().address
     datalist = []
     for imagepath in glob.glob(address + "/*.*"):
       image = Image.open(imagepath)
       #图片的dhash值
       h = str(imagehash.dhash(image))
       hashClass = HashClass(h,imagepath)
       datalist.append(hashClass)
     return json.dumps(datalist, default=lambda o: o.__dict__, sort_keys=True, indent=4)


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()


这里是根据输入的路径返回,当前路径下所有文件(该目录只有png,jpg两种文件)的dhash值和文件地址(需要的库:pip install pillow imagehash)


执行 python index3.py 1235   (可不指定端口号,默认8080)


浏览器下访问http://0.0.0.0:1235/dhash?address=/opt/environments/other 可看到




root@ubuntu:~# ros2 launch origincar_bringup usb_websocket_display.launch.py [INFO] [launch]: All log files can be found below /root/.ros/log/2025-07-25-01-28-47-042580-ubuntu-21917 [INFO] [launch]: Default logging verbosity is set to INFO launch webserver pwd_path is /root webserver_path is /opt/tros/lib/websocket/webservice launch webserver cmd is ./sbin/nginx -p . [INFO] [hobot_usb_cam-1]: process started with pid [22043] [INFO] [hobot_codec_republish-2]: process started with pid [22045] [INFO] [example-3]: process started with pid [22047] [INFO] [websocket-4]: process started with pid [22049] [example-3] [WARN] [1753378128.623308837] [example]: This is dnn node example! [example-3] [WARN] [1753378128.893627052] [example]: Parameter: [example-3] feed_type(0:local, 1:sub): 1 [example-3] image: config/test.jpg [example-3] image_type: 0 [example-3] dump_render_img: 0 [example-3] is_shared_mem_sub: 1 [example-3] config_file: config/fcosworkconfig.json [example-3] msg_pub_topic_name_: hobot_dnn_detection [example-3] [ERROR] [1753378128.894393555] [fcos_detection_parser]: class_names length 2 is not equal to class_num 80 [example-3] [ERROR] [1753378128.894605055] [example]: Load fcos Parser config file fail [example-3] [ERROR] [1753378128.894688056] [example]: Load config fail! [example-3] terminate called after throwing an instance of 'rclcpp::exceptions::RCLError' [example-3] what(): Failed to create interrupt guard condition in Executor constructor: the given context is not valid, either rcl_init() was not called or rcl_shutdown() was called., at /mnt/disk1/zhuo.wang/build_deb/cc_ws_x3/tros_ws/src/tros/rcl/rcl/src/rcl/guard_condition.c:67 [ERROR] [example-3]: process has died [pid 22047, exit code -6, cmd '/opt/tros/lib/dnn_node_example/example --ros-args --log-level warn --ros-args --params-file /tmp/launch_params_ox9r8mk0 --params-file /tmp/launch_params_i9i0o96n --params-file /tmp/launch_params_ts7uyxtl --params-file /tmp/launch_params_rs35_wo5 --params-file /tmp/launch_params_cm8eo5ma']. [hobot_usb_cam-1] [WARN] [1753378129.631290138] [hobot_usb_cam]: Camera calibration file: [/opt/tros/lib/hobot_usb_cam/config/usb_camera_calibration.yaml] does not exist! [hobot_usb_cam-1] If you need calibration msg, please make sure the calibration file path is correct and the calibration file exists! [hobot_usb_cam-1] [WARN] [1753378129.632590059] [hobot_usb_cam]: get camera calibration parameters failed [hobot_usb_cam-1] [WARN] [1753378129.632960769] [hobot_usb_cam]: Start to open device /dev/video8. [hobot_usb_cam-1] [WARN] [1753378129.710134206] [hobot_usb_cam]: Open & Init device /dev/video8 success.
最新发布
07-26
rosroot@ubuntu:~# ros2 launch origincar_bringup usb_websocket_display.launch.py [INFO] [launch]: All log files can be found below /root/.ros/log/2025-07-25-00-30-32-000127-ubuntu-10457 [INFO] [launch]: Default logging verbosity is set to INFO launch webserver pwd_path is /root webserver_path is /opt/tros/lib/websocket/webservice launch webserver cmd is ./sbin/nginx -p . [INFO] [hobot_usb_cam-1]: process started with pid [10730] [INFO] [hobot_codec_republish-2]: process started with pid [10732] [INFO] [example-3]: process started with pid [10734] [INFO] [websocket-4]: process started with pid [10736] [example-3] [WARN] [1753374633.587285650] [example]: This is dnn node example! [example-3] [WARN] [1753374633.763922258] [example]: Parameter: [example-3] feed_type(0:local, 1:sub): 1 [example-3] image: config/test.jpg [example-3] image_type: 0 [example-3] dump_render_img: 0 [example-3] is_shared_mem_sub: 1 [example-3] config_file: config/fcosworkconfig.json [example-3] msg_pub_topic_name_: hobot_dnn_detection [example-3] [ERROR] [1753374633.765273884] [fcos_detection_parser]: class_names length 2 is not equal to class_num 80 [example-3] [ERROR] [1753374633.772209015] [example]: Load fcos Parser config file fail [example-3] [ERROR] [1753374633.772677390] [example]: Load config fail! [example-3] terminate called after throwing an instance of 'rclcpp::exceptions::RCLError' [example-3] what(): Failed to create interrupt guard condition in Executor constructor: the given context is not valid, either rcl_init() was not called or rcl_shutdown() was called., at /mnt/disk1/zhuo.wang/build_deb/cc_ws_x3/tros_ws/src/tros/rcl/rcl/src/rcl/guard_condition.c:67 [ERROR] [example-3]: process has died [pid 10734, exit code -6, cmd '/opt/tros/lib/dnn_node_example/example --ros-args --log-level warn --ros-args --params-file /tmp/launch_params__qq5sikn --params-file /tmp/launch_params_kuzvkrxa --params-file /tmp/launch_params_tng3_8d8 --params-file /tmp/launch_params_tit1364h --params-file /tmp/launch_params_g37rwcrp']. [hobot_usb_cam-1] [WARN] [1753374633.815094676] [hobot_usb_cam]: Camera calibration file: [/opt/tros/lib/hobot_usb_cam/config/usb_camera_calibration.yaml] does not exist! [hobot_usb_cam-1] If you need calibration msg, please make sure the calibration file path is correct and the calibration file exists! [hobot_usb_cam-1] [WARN] [1753374633.816883511] [hobot_usb_cam]: get camera calibration parameters failed [hobot_usb_cam-1] [WARN] [1753374633.817144553] [hobot_usb_cam]: Start to open device /dev/video8. [hobot_usb_cam-1] [WARN] [1753374633.894102243] [hobot_usb_cam]: Open & Init device /dev/video8 success.
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值