huggingface / huggingface/pixparse

[Suggestion] Remove crop_margin dependency on cv2

Open
#22 6 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
25
Forks
4
PR merge metrics
No merged PRs in 30d

Description

Currently we depend on cv2 for Nougat's crop margin, what do you think about something like this, I just changed the two calls to cv2 to this:

```python
def pythonfindNonZero(gray):
non_zero_indices = np.column_stack(np.nonzero(gray))
idxvec = non_zero_indices[:, [1, 0]]
return idxvec

def pythonBoundingRect(coords):
min_vals = np.min(coords, axis=0).astype(int)
max_vals = np.max(coords, axis=0).astype(int)
return min_vals[0], min_vals[1], max_vals[0] - min_vals[0], max_vals[1] - min_vals[1]

class pythonCropMargin:
def __init__(self):
pass

def __call__(self, img):
if isinstance(img, torch.Tensor):
assert False
else:
data = np.array(img.convert("L"))
data = data.astype(np.uint8)
max_val = data.max()
min_val = data.min()
if max_val == min_val:
return img
data = (data - min_val) / (max_val - min_val) * 255
gray = 255 * (data < 200).astype(np.uint8)

coords = pythonfindNonZero(gray)
a, b, w, h = pythonBoundingRect(coords)
return img.crop((a, b, w + a, h + b))
```

This is less efficient than cv2 (21ms on average on my machine vs 12ms for cv2 impl). Does use numpy. I have slightly different resulting images (one pixel on either axis), inducing a slightly different mean/std but overall looks similar

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.