美文网首页
【python3】问题记录

【python3】问题记录

作者: 下里巴人也 | 来源:发表于2018-03-14 10:10 被阅读107次

    参考:http://www.liujiangblog.com/course/python/76

    今天把python2的代码修改兼容python3的几个问题:

    • from Queue import Queue修改为from queue import Queue
    • import thread修改为import _thread
    • print修改
    • socket修改:python3发消息需要encode消息buf,接收消息需要decode消息buf:

    接收消息:

    flag, msg_buf = self.__tcp_client.recv(512)
    logger.info("recv msg from mc: %s", msg_buf)
    msg_buf = msg_buf.decode()
    

    发送消息:

    flag = self.__tcp_client.send(msg.encode())
    
    • opencv库 imshow在线程里调用,会有如下问题:
    [xcb] Unknown request in queue while dequeuing
    [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
    [xcb] Aborting, sorry about that.
    python: xcb_io.c:165: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
    Aborted (core dumped)
    

    原因是:线程里使用imshow问题
    解决方法:
    显示代码:

    cv2.putText(output_rgb,
                                    "FPS: %f" % (fps),
                                    (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                    (0, 255, 0), 2)
                        cv2.imshow(video_name, output_rgb)
                        fps_time = time.time()
                        if cv2.waitKey(1) & 0xFF == ord('q'):
                            logger.warn("exit for waitKey!")
                            break
    

    修改为(imshow之前先调下cv2.namedWindow()):

    cv2.putText(output_rgb,
                                    "FPS: %f" % (fps),
                                    (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                    (0, 255, 0), 2)
                        cv2.namedWindow(video_name)
                        cv2.imshow(video_name, output_rgb)
                        fps_time = time.time()
                        if cv2.waitKey(1) & 0xFF == ord('q'):
                            logger.warn("exit for waitKey!")
                            break
    
    • python3的二维列表使用问题

    相关文章

      网友评论

          本文标题:【python3】问题记录

          本文链接:https://www.haomeiwen.com/subject/slcnfftx.html