Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detect.py --view-img for non-ASCII paths #7093

Merged
merged 15 commits into from
Mar 25, 2022

Conversation

Zengyf-CVer
Copy link
Contributor

@Zengyf-CVer Zengyf-CVer commented Mar 21, 2022

@glenn-jocher
I found a solution to the problem mentioned in #7075, the reason is that my yolov5 project path includes Chinese, so cv2.imshow(str(p), im0) This line of code cannot display the window, resulting in the inability to display pictures and videos, I will fix this bug.

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Improved OpenCV functions for better Chinese character handling in file paths.

📊 Key Changes

  • Added a wrapper function around cv2.imshow to prevent recursion errors.
  • Extended functionality to encode file paths for proper handling of Chinese characters within the imshow function.
  • Redefined cv2.imread, cv2.imwrite, and cv2.imshow to utilize the new, improved functions supporting Chinese character file paths.

🎯 Purpose & Impact

  • 🚀 Purpose: The main goal is to enhance compatibility with Chinese characters in file paths when using OpenCV functions, mitigating issues faced by users with non-Latin alphabets in their file system.
  • 🌍 Impact: This change makes the YOLOv5 more accessible and user-friendly for a global audience, particularly assisting Chinese-speaking developers and users in running image processing tasks without encountering errors related to file path encoding.

@Zengyf-CVer Zengyf-CVer changed the title Fix --view-img Chinese path display problem in opencv form Fix --view-img Chinese path display problem in opencv window Mar 21, 2022
@glenn-jocher
Copy link
Member

@Zengyf-CVer thanks for the PR! I think we should use the strategy I raised in #7139 (comment) for this

@glenn-jocher
Copy link
Member

@Zengyf-CVer looks like unicode escape is faster than is_chinese, so makes sense just to always apply it rather than to check first:

p = 'acbadsfasdfasdfadslfkhadsfhjlasdfhljj.jpg'

%timeit p.encode('unicode_escape')
462 ns ± 3.47 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
%timeit is_chinese(p)
1.15 µs ± 11.3 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 25, 2022

@Zengyf-CVer ok I've updated per my recommendation and replaced

-    cv2.imshow(p.encode('unicode_escape').decode(), im)
+    cv2.imshow(p.encode().decode('utf-8', 'ignore'), im)

Does this work for your use case? If not can you send me the image or string that is causing the problem?

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
Still can't display properly, here is my video path and result:

# video path:
/home/zyf/文档/yolov5/video/202202241928.mp4
# run:
python detect.py --weights ./yolov5s.pt --source ./video/ --view-img

ksnip_20220325-195704

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 25, 2022

@Zengyf-CVer here are my --view-img results on MacOS. I renamed zidane.jpg as 文档.jpg. Not sure what to do.

path.encode('unicode_escape').decode()

Screenshot 2022-03-25 at 13 06 48

path.encode('').decode('utf-8', 'ignore')

Screenshot 2022-03-25 at 13 08 20

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
My result is also like this, the code will convert Chinese to a specific encoding, but at least the picture can be displayed normally.

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
This works fine on Ubuntu:

imshow_(path.encode('unicode_escape').decode(), im)

But this doesn't show up:

imshow_(path.encode().decode('utf-8', 'ignore'), im)

@glenn-jocher glenn-jocher changed the title Fix --view-img Chinese path display problem in opencv window Fix detect.py --view-img for non-ASCII paths Mar 25, 2022
@glenn-jocher
Copy link
Member

@Zengyf-CVer got it. I've reverted to imshow_(path.encode().decode('utf-8', 'ignore'), im). Should be good to merge once CI completes.

@glenn-jocher glenn-jocher merged commit a4c6618 into ultralytics:master Mar 25, 2022
@glenn-jocher
Copy link
Member

@Zengyf-CVer PR is merged. Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐

@glenn-jocher glenn-jocher linked an issue Mar 25, 2022 that may be closed by this pull request
1 task
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
* Update detect.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update general.py

* Update detect.py

* Update general.py

* Update general.py

* Update general.py

* Update general.py

* Update general.py

* Update general.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update general.py

* Update general.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

About real-time display of video files
2 participants