Fix cv2.imwrite on non-ASCII paths#7139
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
👋 Hello @CCRcmcpe, thank you for submitting a YOLOv5 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
- ✅ Verify your PR is up-to-date with upstream/master. If your PR is behind upstream/master an automatic GitHub Actions merge may be attempted by writing /rebase in a new comment, or by running the following code, replacing 'feature' with the name of your local branch:
git remote add upstream https://github.com/ultralytics/yolov5.git
git fetch upstream
# git checkout feature # <--- replace 'feature' with local branch name
git merge upstream/master
git push -u origin -f- ✅ Verify all Continuous Integration (CI) checks are passing.
- ✅ Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." -Bruce Lee
|
@CCRcmcpe ok got it. What we should do then is this. Create the two functions in general.py and then assign them to cv2: def imread(path):
return cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_COLOR)
def imwrite(path, img):
try:
cv2.imencode(Path(path).suffix, img)[1].tofile(path)
return True
except:
return False
cv2.imread, cv2.imwrite = imread, imwriteThen everywhere that imports cv2, change it to: from utils.general import cv2And finally delete all other changes from #6979 and this PR. Then everything should work well with a minimum of customizations to the code. |
imwrite on non-ASCII pathscv2.imwrite on non-ASCII paths
|
@CCRcmcpe PR is merged. Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐ |
Great! I'm relatively new to python so I don't know can actually import like this. |
|
@CCRcmcpe it's a bit of an experimental approach, but yes you can overwrite imports, or most any other variable/method after they are defined. |
* Fix imwrite on non-ASCII paths * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update general.py * Update __init__.py * Update __init__.py * Update datasets.py * Update hubconf.py * Update detect.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * 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>


Previous PR #6979 fixed
imread. But I discoveredimwriteis also broken: when saving a image, Unicode characters in the path are converted to meaningless characters, or it does not save the image at all.Also moved the custom
imreadtoutils.general.🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Refactoring OpenCV image read/write functions for compatibility with non-latin filenames.
📊 Key Changes
import cv2statements from multiple files (detect.py,hubconf.py, anddatasets.py).cv2.imreadandcv2.imwritefunctions with custom Chinese-friendly versions inutils/general.py.🎯 Purpose & Impact