Skip to content

Commit

Permalink
Remove Python 2 compatibility dependency six (#14202)
Browse files Browse the repository at this point in the history
* Remove Python 2 compatibility dependency

* Remove Python 2 compatibility dependency six

* Update operators.py

* Remove Python 2 compatibility dependency six
  • Loading branch information
cclauss authored Nov 12, 2024
1 parent 2c846ef commit 9b92a1c
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 41 deletions.
10 changes: 1 addition & 9 deletions ppocr/data/imaug/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from __future__ import unicode_literals

import sys
import six
import cv2
import numpy as np
import math
Expand All @@ -39,14 +38,7 @@ def __init__(

def __call__(self, data):
img = data["image"]
if six.PY2:
assert (
type(img) is str and len(img) > 0
), "invalid input 'img' in DecodeImage"
else:
assert (
type(img) is bytes and len(img) > 0
), "invalid input 'img' in DecodeImage"
assert type(img) is bytes and len(img) > 0, "invalid input 'img' in DecodeImage"
img = np.frombuffer(img, dtype="uint8")
if self.ignore_orientation:
img = cv2.imdecode(img, cv2.IMREAD_IGNORE_ORIENTATION | cv2.IMREAD_COLOR)
Expand Down
11 changes: 2 additions & 9 deletions ppocr/data/imaug/randaugment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from PIL import Image, ImageEnhance, ImageOps
import numpy as np
import random
import six


class RawRandAugment(object):
Expand Down Expand Up @@ -117,10 +116,7 @@ class RandAugment(RawRandAugment):

def __init__(self, prob=0.5, *args, **kwargs):
self.prob = prob
if six.PY2:
super(RandAugment, self).__init__(*args, **kwargs)
else:
super().__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def __call__(self, data):
if np.random.rand() > self.prob:
Expand All @@ -130,10 +126,7 @@ def __call__(self, data):
img = np.ascontiguousarray(img)
img = Image.fromarray(img)

if six.PY2:
img = super(RandAugment, self).__call__(img)
else:
img = super().__call__(img)
img = super().__call__(img)

if isinstance(img, Image.Image):
img = np.asarray(img)
Expand Down
1 change: 0 additions & 1 deletion ppocr/data/imaug/table_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from __future__ import unicode_literals

import sys
import six
import cv2
import numpy as np

Expand Down
4 changes: 2 additions & 2 deletions ppocr/data/lmdb_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import io
import os
from paddle.io import Dataset
import lmdb
import cv2
import string
import six
import pickle
from PIL import Image

Expand Down Expand Up @@ -158,7 +158,7 @@ def __len__(self):
class LMDBDataSetSR(LMDBDataSet):
def buf2PIL(self, txn, key, type="RGB"):
imgbuf = txn.get(key)
buf = six.BytesIO()
buf = io.BytesIO()
buf.write(imgbuf)
buf.seek(0)
im = Image.open(buf).convert(type)
Expand Down
9 changes: 2 additions & 7 deletions ppocr/utils/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import errno
import os
import pickle
import six
import json

import paddle
Expand Down Expand Up @@ -69,9 +68,7 @@ def load_model(config, model, optimizer=None, model_type="det"):
if checkpoints:
if os.path.exists(os.path.join(checkpoints, "metric.states")):
with open(os.path.join(checkpoints, "metric.states"), "rb") as f:
states_dict = (
pickle.load(f) if six.PY2 else pickle.load(f, encoding="latin1")
)
states_dict = pickle.load(f, encoding="latin1")
best_model_dict = states_dict.get("best_model_dict", {})
if "epoch" in states_dict:
best_model_dict["start_epoch"] = states_dict["epoch"] + 1
Expand Down Expand Up @@ -140,9 +137,7 @@ def load_model(config, model, optimizer=None, model_type="det"):

if os.path.exists(checkpoints + ".states"):
with open(checkpoints + ".states", "rb") as f:
states_dict = (
pickle.load(f) if six.PY2 else pickle.load(f, encoding="latin1")
)
states_dict = pickle.load(f, encoding="latin1")
best_model_dict = states_dict.get("best_model_dict", {})
best_model_dict["acc"] = 0.0
if "epoch" in states_dict:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ classifiers = [
dependencies = [
"shapely",
"scikit-image",
"six",
"pyclipper",
"lmdb",
"tqdm",
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
shapely
scikit-image
six
pyclipper
lmdb
tqdm
Expand Down
9 changes: 1 addition & 8 deletions test_tipc/supplementary/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ def __init__(self, img_mode="RGB", channel_first=False, **kwargs):

def __call__(self, data):
img = data["image"]
if six.PY2:
assert (
type(img) is str and len(img) > 0
), "invalid input 'img' in DecodeImage"
else:
assert (
type(img) is bytes and len(img) > 0
), "invalid input 'img' in DecodeImage"
assert type(img) is bytes and len(img) > 0, "invalid input 'img' in DecodeImage"
img = np.frombuffer(img, dtype="uint8")
img = cv2.imdecode(img, 1)
if img is None:
Expand Down
4 changes: 1 addition & 3 deletions test_tipc/supplementary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def load_model(config, model, optimizer=None):

if os.path.exists(checkpoints + ".states"):
with open(checkpoints + ".states", "rb") as f:
states_dict = (
pickle.load(f) if six.PY2 else pickle.load(f, encoding="latin1")
)
states_dict = pickle.load(f, encoding="latin1")
best_model_dict = states_dict.get("best_model_dict", {})
if "epoch" in states_dict:
best_model_dict["start_epoch"] = states_dict["epoch"] + 1
Expand Down

0 comments on commit 9b92a1c

Please sign in to comment.