Update app.py
Browse files
app.py
CHANGED
|
@@ -1891,114 +1891,34 @@ with gr.Blocks(title="Certificate & Egyptian ID Card Cropper") as interface:
|
|
| 1891 |
gr.Markdown("## πͺπ¬ Egyptian ID Card Cropper")
|
| 1892 |
gr.Markdown("Upload an Egyptian ID photograph or a ZIP containing multiple images.")
|
| 1893 |
|
| 1894 |
-
|
| 1895 |
-
|
| 1896 |
-
|
| 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 |
-
|
| 1908 |
-
|
| 1909 |
-
|
| 1910 |
-
|
| 1911 |
-
|
| 1912 |
-
|
| 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 |
-
|
| 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 |
-
|
| 1986 |
-
|
| 1987 |
-
|
| 1988 |
-
|
| 1989 |
-
)
|
| 1990 |
|
| 1991 |
-
|
| 1992 |
-
|
| 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")
|
|
|
|
| 1891 |
gr.Markdown("## πͺπ¬ Egyptian ID Card Cropper")
|
| 1892 |
gr.Markdown("Upload an Egyptian ID photograph or a ZIP containing multiple images.")
|
| 1893 |
|
| 1894 |
+
gr.Markdown("### Single Image")
|
| 1895 |
+
input_image = gr.Image(type="filepath", label="Upload ID Image")
|
| 1896 |
+
process_button = gr.Button("βοΈ Crop ID Card", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1897 |
|
| 1898 |
+
with gr.Row():
|
| 1899 |
+
with gr.Column():
|
| 1900 |
+
output_image = gr.Image(
|
| 1901 |
+
type="numpy",
|
| 1902 |
+
label="πͺͺ New Cropped ID Card Preview",
|
| 1903 |
+
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1904 |
)
|
| 1905 |
+
with gr.Column():
|
| 1906 |
+
output_file = gr.File(
|
| 1907 |
+
label="β¬οΈ Download Cropped ID Card (JPG)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1908 |
)
|
| 1909 |
|
| 1910 |
+
gr.Markdown("### Image Controls")
|
| 1911 |
+
brightness_slider = gr.Slider(minimum=-50, maximum=50, value=10, step=1, label="Brightness", info="0 = original. Positive values make the ID brighter.")
|
| 1912 |
+
rotation_dropdown = gr.Dropdown(choices=[0, 90, 180, 270], value=0, label="Rotate Image", info="Rotation is applied clockwise.")
|
| 1913 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 1914 |
|
| 1915 |
+
process_outputs = [output_image, output_file, status]
|
| 1916 |
+
process_inputs = [input_image, brightness_slider, rotation_dropdown]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1917 |
|
| 1918 |
+
process_button.click(process_image, process_inputs, process_outputs)
|
| 1919 |
+
input_image.change(process_image, process_inputs, process_outputs)
|
| 1920 |
+
brightness_slider.change(process_image, process_inputs, process_outputs)
|
| 1921 |
+
rotation_dropdown.change(process_image, process_inputs, process_outputs)
|
| 1922 |
|
| 1923 |
gr.Markdown("---")
|
| 1924 |
gr.Markdown("### π¦ Process a ZIP")
|