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

The notebook of Preprocessor class #60

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

priyanshi-git
Copy link

I am facing a different issue now i.e., although I installed ocred package the Preprocessor is not being imported it shows an ImportError

@codecov
Copy link

codecov bot commented Aug 12, 2022

Codecov Report

Merging #60 (23ba713) into main (420aa7a) will decrease coverage by 0.08%.
The diff coverage is n/a.

❗ Current head 23ba713 differs from pull request most recent head c3e778a. Consider uploading reports for the commit c3e778a to get more accurate results

@@            Coverage Diff             @@
##             main      #60      +/-   ##
==========================================
- Coverage   97.09%   97.00%   -0.09%     
==========================================
  Files           3        3              
  Lines         172      167       -5     
==========================================
- Hits          167      162       -5     
  Misses          5        5              
Impacted Files Coverage Δ
ocred/ocr.py 95.14% <0.00%> (-0.23%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Saransh-cpp
Copy link
Owner

@priyanshi-git can you try !pip install -U ocred --no-cache --force-reinstall?

@priyanshi-git
Copy link
Author

priyanshi-git commented Aug 12, 2022

@priyanshi-git can you try !pip install -U ocred --no-cache --force-reinstall?

Yes this worked. Now the issue of displaying the result persists. The code shows the output as True and do not display the preprocessed.png image on its own.

@Saransh-cpp
Copy link
Owner

Nice!! Page.png is already preprocessed; hence, I think the Preprocessor class won't change it much. Could you try using a different image? Perhaps one of the Cosmos ones.

Additionally, the notebook should be broken up into smaller cells -

preprocessed = Preprocessor("/content/Images/Page.png")

# scan the image and copy the scanned image
scanned = preprocessed.scan(inplace=True)
orig = scanned.copy()

# display scanned image here

# remove noise
noise_free = preprocessed.remove_noise(
    inplace=True, overriden_image=scanned
)

# display noiseless image here

# thicken the ink to draw Hough lines better
thickened = preprocessed.thicken_font(
    inplace=True, overriden_image=noise_free
)

# display thickened image here

and so on...

@Saransh-cpp Saransh-cpp self-requested a review August 13, 2022 11:55
@Saransh-cpp Saransh-cpp added the documentation Improvements or additions to documentation label Aug 13, 2022
@priyanshi-git
Copy link
Author

Hey !! I made the changes and everything seems fine except the last step and I don't understand why that's happening. Let me know what can be done

@Saransh-cpp
Copy link
Owner

The remove_noise call outputs the original image (which it should not) as the return value of the method was wrong. This has been fixed here - #62.

I will make a new release today, which should fix it in the PyPI version,

@Saransh-cpp
Copy link
Owner

@all-contributors please add @priyanshi-git for bug

@allcontributors
Copy link
Contributor

@Saransh-cpp

I've put up a pull request to add @priyanshi-git! 🎉

@Saransh-cpp
Copy link
Owner

Merge conflict: @all-contributors please add @priyanshi-git for bug

@allcontributors
Copy link
Contributor

@Saransh-cpp

@priyanshi-git already contributed before to bug

@Saransh-cpp
Copy link
Owner

#65

@priyanshi-git
Copy link
Author

Hey @Saransh-cpp I guess its working perfectly now

@Saransh-cpp
Copy link
Owner

Looks good now! Could you convert this to a markdown file and upload it in this branch? Just copy-pasting the Jupyter notebook content, would be better for the reviews!

@priyanshi-git
Copy link
Author

Hey @Saransh-cpp, I created the notebook for examples of OCR, but the output of signboard doesn't look fine to me, some characters are wrong, the same was happening with the invoice example as well.

@Saransh-cpp
Copy link
Owner

Hey @Saransh-cpp, I created the notebook for examples of OCR, but the output of signboard doesn't look fine to me, some characters are wrong, the same was happening with the invoice example as well.

Some characters will always be wrong, don't worry about them. I'll try to find a workaround for the failing check.

Copy link
Owner

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this, @priyanshi-git! Apologies for the late review, and the large review :) The overall structure of the example looks good! I have addressed some changes below -

examples/Preprocessing_example.md Outdated Show resolved Hide resolved
import cv2
from scipy import ndimage
from ocred import Preprocessor
from google.colab.patches import cv2_imshow
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use cv2 or matplotlib to show images in Jupyter. (cv2.imshow should work and this import can be removed)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did use cv2.imshow initially but it gave an error that this function is not functional anymore in colab, hence I had to import the new function separately and use it.

examples/Preprocessing_example.md Outdated Show resolved Hide resolved
examples/Preprocessing_example.md Outdated Show resolved Hide resolved
examples/Preprocessing_example.md Outdated Show resolved Hide resolved
examples/Preprocessing_example.md Outdated Show resolved Hide resolved
Comment on lines +61 to +77
```
# rotate the original scanned image
rotated = ndimage.rotate(orig, median_angle)

cv2_imshow(rotated)
```

![image](https://user-images.githubusercontent.com/82112540/184902055-61c50a47-7894-4d2d-aba7-89acfa4b53f1.png)

```
# remove noise again
final_img = preprocessed.remove_noise(inplace=True, overriden_image=rotated)

cv2_imshow(final_img)
```

![image](https://user-images.githubusercontent.com/82112540/184902140-42582eac-e765-44b6-b518-b8aa81ada09c.png)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove these, and add the inplace=True example.

Notice how we don't need return values now, given that the functions are altering the pre_img variable directly, without copying it, making the code efficient!

Would be nice to split this into cells too!

preprocessed = Preprocessor("path/to/image")

# scan the image and copy the scanned image
pre_img = preprocessed.scan(inplace=True)

# plot pre_img

# remove noise
preprocessed.remove_noise(
    inplace=True, overriden_image=pre_img  # the variable `pre_img` is altered 'inplace'
)

# plot pre_img

# thicken the ink to draw Hough lines better
preprocessed.thicken_font(
    inplace=True, overriden_image=pre_img
)

# plot pre_img

# calculate the median angle of all the Hough lines
preprocessed.rotate(
    inplace=True, overriden_image=pre_img
)

# plot pre_img

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I use to plot the image ? Also, I think we need variable to save the changes and then plot the image because although we put inplace=True when I tried to plot using cv2_imshow(pre-img) the outputs didn't have any changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I use to plot the image ? Also, I think we need variable to save the changes and then plot the image because although we put inplace=True when I tried to plot using cv2_imshow(pre-img) the outputs didn't have any changes.

This looks like a bug. Can you open a new issue for this? I will fix it this weekend probably. This is why sometimes I think it would have been better if I wrote ocred in julia.

examples/Preprocessing_example.md Outdated Show resolved Hide resolved
from google.colab.patches import cv2_imshow


preprocessed = Preprocessor("/content/images/CosmosOne.jpg")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the users will be using Jupyter locally. You can make this change at the end, when you are done with running this code on colab!

Suggested change
preprocessed = Preprocessor("/content/images/CosmosOne.jpg")
preprocessed = Preprocessor("../images/CosmosOne.jpg")

@Saransh-cpp
Copy link
Owner

You can directly commit the suggestions here, and later propagate it to the notebook manually!

@Saransh-cpp
Copy link
Owner

@priyanshi-git, #71 fixes the bug! Turns out cv2 does not let you alter inplace in Python (it does allow the same in C++, a bit weird). I had to get rid of the inplace and overriden_image argument to make everything sane again, and v0.3.0 is out with these changes.

This should work now -

# manually preprocessing an image
import cv2
from scipy import ndimage
from ocred import Preprocessor

preprocessed = Preprocessor("path/to/img.jpg")
# display preprocessor.img -> it should be the passed in image

# scan the image and copy the scanned image
preprocessed.scan()
# display preprocessed.img -> it should be scanned

orig = preprocessed.img.copy()

# remove noise
preprocessed.remove_noise()
# display preprocessed.img -> it should be noiseless

# thicken the ink to draw Hough lines better
preprocessed.thicken_font()
# display preprocessed.img -> it should be thickened

# calculate the median angle of all the Hough lines
_, median_angle = preprocessed.rotate()
# display preprocessed.img -> it should be rotated

# rotate the original scanned image
rotated = ndimage.rotate(orig, median_angle)
# display rotated -> the original scanned image should be rotated

# remove noise again
preprocessed = Preprocessor(rotated)
preprocessed.remove_noise()
# display prepreocessed.img-> the original scanned and rotated image should be noiseless

cv2.imwrite("preprocessed.png", preprocessed.img)

@Saransh-cpp
Copy link
Owner

So now, we can get rid of the two sections which were editing inplace and editing a copy. A single section with the example above should work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants