Update app.py
Browse files
app.py
CHANGED
|
@@ -1569,26 +1569,30 @@ def process_image(
|
|
| 1569 |
try:
|
| 1570 |
if file is None:
|
| 1571 |
return (
|
|
|
|
| 1572 |
None,
|
| 1573 |
"Please upload an ID image."
|
| 1574 |
)
|
| 1575 |
-
|
| 1576 |
-
|
| 1577 |
-
)
|
| 1578 |
if image is None:
|
| 1579 |
return (
|
|
|
|
| 1580 |
None,
|
| 1581 |
"Could not decode image."
|
| 1582 |
)
|
|
|
|
| 1583 |
cropped = crop_id_card(
|
| 1584 |
image,
|
| 1585 |
brightness
|
| 1586 |
)
|
| 1587 |
if cropped is None:
|
| 1588 |
return (
|
|
|
|
| 1589 |
None,
|
| 1590 |
"β ID card was not detected."
|
| 1591 |
)
|
|
|
|
| 1592 |
# ----------------------------------------------------
|
| 1593 |
# USER ROTATION
|
| 1594 |
# ----------------------------------------------------
|
|
@@ -1608,12 +1612,20 @@ def process_image(
|
|
| 1608 |
cropped,
|
| 1609 |
cv2.ROTATE_90_COUNTERCLOCKWISE
|
| 1610 |
)
|
|
|
|
| 1611 |
# ----------------------------------------------------
|
| 1612 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1613 |
# ----------------------------------------------------
|
| 1614 |
-
# Returning a NumPy/PIL image to Gradio can cause Gradio
|
| 1615 |
-
# to re-encode it as WEBP. Save the bytes ourselves and
|
| 1616 |
-
# return the actual .jpg filepath instead.
|
| 1617 |
fd, jpg_path = tempfile.mkstemp(
|
| 1618 |
prefix="id_card_crop_",
|
| 1619 |
suffix=".jpg"
|
|
@@ -1635,23 +1647,28 @@ def process_image(
|
|
| 1635 |
except OSError:
|
| 1636 |
pass
|
| 1637 |
return (
|
|
|
|
| 1638 |
None,
|
| 1639 |
"β Could not save cropped ID card as JPG."
|
| 1640 |
)
|
| 1641 |
|
| 1642 |
return (
|
|
|
|
| 1643 |
jpg_path,
|
| 1644 |
-
"β
ID card cropped successfully. JPG saved."
|
| 1645 |
)
|
|
|
|
| 1646 |
except Exception as e:
|
| 1647 |
print(
|
| 1648 |
"PROCESS ERROR:",
|
| 1649 |
repr(e)
|
| 1650 |
)
|
| 1651 |
return (
|
|
|
|
| 1652 |
None,
|
| 1653 |
"β Error: " + str(e)
|
| 1654 |
)
|
|
|
|
| 1655 |
# ============================================================
|
| 1656 |
# ZIP PROCESSING
|
| 1657 |
# ============================================================
|
|
@@ -1874,18 +1891,114 @@ with gr.Blocks(title="Certificate & Egyptian ID Card Cropper") as interface:
|
|
| 1874 |
gr.Markdown("## πͺπ¬ Egyptian ID Card Cropper")
|
| 1875 |
gr.Markdown("Upload an Egyptian ID photograph or a ZIP containing multiple images.")
|
| 1876 |
|
| 1877 |
-
|
| 1878 |
-
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
| 1882 |
-
|
| 1883 |
-
|
| 1884 |
-
|
| 1885 |
-
|
| 1886 |
-
|
| 1887 |
-
|
| 1888 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1889 |
|
| 1890 |
gr.Markdown("---")
|
| 1891 |
gr.Markdown("### π¦ Process a ZIP")
|
|
|
|
| 1569 |
try:
|
| 1570 |
if file is None:
|
| 1571 |
return (
|
| 1572 |
+
None,
|
| 1573 |
None,
|
| 1574 |
"Please upload an ID image."
|
| 1575 |
)
|
| 1576 |
+
|
| 1577 |
+
image = read_gradio_image(file)
|
|
|
|
| 1578 |
if image is None:
|
| 1579 |
return (
|
| 1580 |
+
None,
|
| 1581 |
None,
|
| 1582 |
"Could not decode image."
|
| 1583 |
)
|
| 1584 |
+
|
| 1585 |
cropped = crop_id_card(
|
| 1586 |
image,
|
| 1587 |
brightness
|
| 1588 |
)
|
| 1589 |
if cropped is None:
|
| 1590 |
return (
|
| 1591 |
+
None,
|
| 1592 |
None,
|
| 1593 |
"β ID card was not detected."
|
| 1594 |
)
|
| 1595 |
+
|
| 1596 |
# ----------------------------------------------------
|
| 1597 |
# USER ROTATION
|
| 1598 |
# ----------------------------------------------------
|
|
|
|
| 1612 |
cropped,
|
| 1613 |
cv2.ROTATE_90_COUNTERCLOCKWISE
|
| 1614 |
)
|
| 1615 |
+
|
| 1616 |
# ----------------------------------------------------
|
| 1617 |
+
# PREVIEW
|
| 1618 |
+
# ----------------------------------------------------
|
| 1619 |
+
# Show the processed crop immediately in the ID section.
|
| 1620 |
+
# This is independent from the downloadable JPG file.
|
| 1621 |
+
cropped_rgb = cv2.cvtColor(
|
| 1622 |
+
cropped,
|
| 1623 |
+
cv2.COLOR_BGR2RGB
|
| 1624 |
+
)
|
| 1625 |
+
|
| 1626 |
+
# ----------------------------------------------------
|
| 1627 |
+
# SAVE AS REAL JPG
|
| 1628 |
# ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
| 1629 |
fd, jpg_path = tempfile.mkstemp(
|
| 1630 |
prefix="id_card_crop_",
|
| 1631 |
suffix=".jpg"
|
|
|
|
| 1647 |
except OSError:
|
| 1648 |
pass
|
| 1649 |
return (
|
| 1650 |
+
cropped_rgb,
|
| 1651 |
None,
|
| 1652 |
"β Could not save cropped ID card as JPG."
|
| 1653 |
)
|
| 1654 |
|
| 1655 |
return (
|
| 1656 |
+
cropped_rgb,
|
| 1657 |
jpg_path,
|
| 1658 |
+
"β
ID card cropped successfully. Preview shown and JPG saved."
|
| 1659 |
)
|
| 1660 |
+
|
| 1661 |
except Exception as e:
|
| 1662 |
print(
|
| 1663 |
"PROCESS ERROR:",
|
| 1664 |
repr(e)
|
| 1665 |
)
|
| 1666 |
return (
|
| 1667 |
+
None,
|
| 1668 |
None,
|
| 1669 |
"β Error: " + str(e)
|
| 1670 |
)
|
| 1671 |
+
|
| 1672 |
# ============================================================
|
| 1673 |
# ZIP PROCESSING
|
| 1674 |
# ============================================================
|
|
|
|
| 1891 |
gr.Markdown("## πͺπ¬ Egyptian ID Card Cropper")
|
| 1892 |
gr.Markdown("Upload an Egyptian ID photograph or a ZIP containing multiple images.")
|
| 1893 |
|
| 1894 |
+
def process_image(
|
| 1895 |
+
file,
|
| 1896 |
+
brightness,
|
| 1897 |
+
rotation
|
| 1898 |
+
):
|
| 1899 |
+
try:
|
| 1900 |
+
if file is None:
|
| 1901 |
+
return (
|
| 1902 |
+
None,
|
| 1903 |
+
None,
|
| 1904 |
+
"Please upload an ID image."
|
| 1905 |
+
)
|
| 1906 |
+
|
| 1907 |
+
image = read_gradio_image(file)
|
| 1908 |
+
if image is None:
|
| 1909 |
+
return (
|
| 1910 |
+
None,
|
| 1911 |
+
None,
|
| 1912 |
+
"Could not decode image."
|
| 1913 |
+
)
|
| 1914 |
+
|
| 1915 |
+
cropped = crop_id_card(
|
| 1916 |
+
image,
|
| 1917 |
+
brightness
|
| 1918 |
+
)
|
| 1919 |
+
if cropped is None:
|
| 1920 |
+
return (
|
| 1921 |
+
None,
|
| 1922 |
+
None,
|
| 1923 |
+
"β ID card was not detected."
|
| 1924 |
+
)
|
| 1925 |
+
|
| 1926 |
+
# ----------------------------------------------------
|
| 1927 |
+
# USER ROTATION
|
| 1928 |
+
# ----------------------------------------------------
|
| 1929 |
+
rotation = int(rotation)
|
| 1930 |
+
if rotation == 90:
|
| 1931 |
+
cropped = cv2.rotate(
|
| 1932 |
+
cropped,
|
| 1933 |
+
cv2.ROTATE_90_CLOCKWISE
|
| 1934 |
+
)
|
| 1935 |
+
elif rotation == 180:
|
| 1936 |
+
cropped = cv2.rotate(
|
| 1937 |
+
cropped,
|
| 1938 |
+
cv2.ROTATE_180
|
| 1939 |
+
)
|
| 1940 |
+
elif rotation == 270:
|
| 1941 |
+
cropped = cv2.rotate(
|
| 1942 |
+
cropped,
|
| 1943 |
+
cv2.ROTATE_90_COUNTERCLOCKWISE
|
| 1944 |
+
)
|
| 1945 |
+
|
| 1946 |
+
# ----------------------------------------------------
|
| 1947 |
+
# PREVIEW
|
| 1948 |
+
# ----------------------------------------------------
|
| 1949 |
+
# Show the processed crop immediately in the ID section.
|
| 1950 |
+
# This is independent from the downloadable JPG file.
|
| 1951 |
+
cropped_rgb = cv2.cvtColor(
|
| 1952 |
+
cropped,
|
| 1953 |
+
cv2.COLOR_BGR2RGB
|
| 1954 |
+
)
|
| 1955 |
+
|
| 1956 |
+
# ----------------------------------------------------
|
| 1957 |
+
# SAVE AS REAL JPG
|
| 1958 |
+
# ----------------------------------------------------
|
| 1959 |
+
fd, jpg_path = tempfile.mkstemp(
|
| 1960 |
+
prefix="id_card_crop_",
|
| 1961 |
+
suffix=".jpg"
|
| 1962 |
+
)
|
| 1963 |
+
os.close(fd)
|
| 1964 |
+
|
| 1965 |
+
success = cv2.imwrite(
|
| 1966 |
+
jpg_path,
|
| 1967 |
+
cropped,
|
| 1968 |
+
[
|
| 1969 |
+
int(cv2.IMWRITE_JPEG_QUALITY),
|
| 1970 |
+
95
|
| 1971 |
+
]
|
| 1972 |
+
)
|
| 1973 |
+
|
| 1974 |
+
if not success:
|
| 1975 |
+
try:
|
| 1976 |
+
os.remove(jpg_path)
|
| 1977 |
+
except OSError:
|
| 1978 |
+
pass
|
| 1979 |
+
return (
|
| 1980 |
+
cropped_rgb,
|
| 1981 |
+
None,
|
| 1982 |
+
"β Could not save cropped ID card as JPG."
|
| 1983 |
+
)
|
| 1984 |
+
|
| 1985 |
+
return (
|
| 1986 |
+
cropped_rgb,
|
| 1987 |
+
jpg_path,
|
| 1988 |
+
"β
ID card cropped successfully. Preview shown and JPG saved."
|
| 1989 |
+
)
|
| 1990 |
+
|
| 1991 |
+
except Exception as e:
|
| 1992 |
+
print(
|
| 1993 |
+
"PROCESS ERROR:",
|
| 1994 |
+
repr(e)
|
| 1995 |
+
)
|
| 1996 |
+
return (
|
| 1997 |
+
None,
|
| 1998 |
+
None,
|
| 1999 |
+
"β Error: " + str(e)
|
| 2000 |
+
)
|
| 2001 |
+
|
| 2002 |
|
| 2003 |
gr.Markdown("---")
|
| 2004 |
gr.Markdown("### π¦ Process a ZIP")
|