Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1907,80 +1907,226 @@ def create_excel(
|
|
| 1907 |
objects_df,
|
| 1908 |
faces_df
|
| 1909 |
):
|
|
|
|
|
|
|
| 1910 |
|
| 1911 |
-
|
|
|
|
|
|
|
|
|
|
| 1912 |
|
|
|
|
| 1913 |
|
| 1914 |
with pd.ExcelWriter(
|
| 1915 |
output,
|
| 1916 |
engine="openpyxl"
|
| 1917 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1918 |
|
|
|
|
|
|
|
|
|
|
| 1919 |
|
| 1920 |
-
|
| 1921 |
-
|
| 1922 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1923 |
|
| 1924 |
-
|
|
|
|
|
|
|
|
|
|
| 1925 |
writer,
|
| 1926 |
sheet_name="Images",
|
| 1927 |
index=False
|
| 1928 |
)
|
| 1929 |
|
| 1930 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1931 |
writer,
|
| 1932 |
sheet_name="Objects",
|
| 1933 |
index=False
|
| 1934 |
)
|
| 1935 |
|
| 1936 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1937 |
writer,
|
| 1938 |
sheet_name="Faces",
|
| 1939 |
index=False
|
| 1940 |
)
|
| 1941 |
|
| 1942 |
|
| 1943 |
-
#
|
| 1944 |
-
#
|
| 1945 |
-
#
|
| 1946 |
|
| 1947 |
if not objects_df.empty:
|
| 1948 |
|
| 1949 |
object_summary = (
|
| 1950 |
-
|
| 1951 |
objects_df
|
| 1952 |
.groupby("object")
|
| 1953 |
.agg(
|
| 1954 |
-
|
| 1955 |
detections=(
|
| 1956 |
"object",
|
| 1957 |
"size"
|
| 1958 |
),
|
| 1959 |
-
|
| 1960 |
mean_confidence=(
|
| 1961 |
"confidence",
|
| 1962 |
"mean"
|
| 1963 |
),
|
| 1964 |
-
|
| 1965 |
mean_area_fraction=(
|
| 1966 |
"area_fraction",
|
| 1967 |
"mean"
|
| 1968 |
),
|
| 1969 |
-
|
| 1970 |
)
|
| 1971 |
-
|
| 1972 |
.sort_values(
|
| 1973 |
"detections",
|
| 1974 |
ascending=False
|
| 1975 |
)
|
| 1976 |
-
|
| 1977 |
.reset_index()
|
| 1978 |
-
|
| 1979 |
)
|
| 1980 |
|
| 1981 |
else:
|
| 1982 |
|
| 1983 |
-
object_summary = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1984 |
|
| 1985 |
|
| 1986 |
object_summary.to_excel(
|
|
@@ -1990,26 +2136,29 @@ def create_excel(
|
|
| 1990 |
)
|
| 1991 |
|
| 1992 |
|
| 1993 |
-
#
|
| 1994 |
-
#
|
| 1995 |
-
#
|
| 1996 |
|
| 1997 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1998 |
|
| 1999 |
objects_by_category = (
|
| 2000 |
-
|
| 2001 |
pd.crosstab(
|
| 2002 |
objects_df["category"],
|
| 2003 |
objects_df["object"]
|
| 2004 |
)
|
| 2005 |
-
|
| 2006 |
.reset_index()
|
| 2007 |
-
|
| 2008 |
)
|
| 2009 |
|
| 2010 |
else:
|
| 2011 |
|
| 2012 |
-
objects_by_category = pd.DataFrame(
|
|
|
|
|
|
|
| 2013 |
|
| 2014 |
|
| 2015 |
objects_by_category.to_excel(
|
|
@@ -2019,26 +2168,32 @@ def create_excel(
|
|
| 2019 |
)
|
| 2020 |
|
| 2021 |
|
| 2022 |
-
#
|
| 2023 |
-
#
|
| 2024 |
-
#
|
| 2025 |
|
| 2026 |
-
if
|
|
|
|
|
|
|
|
|
|
| 2027 |
|
| 2028 |
faces_by_category = (
|
| 2029 |
-
|
| 2030 |
faces_df
|
| 2031 |
.groupby("category")
|
| 2032 |
.size()
|
| 2033 |
.reset_index(
|
| 2034 |
name="detected_faces"
|
| 2035 |
)
|
| 2036 |
-
|
| 2037 |
)
|
| 2038 |
|
| 2039 |
else:
|
| 2040 |
|
| 2041 |
-
faces_by_category = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2042 |
|
| 2043 |
|
| 2044 |
faces_by_category.to_excel(
|
|
@@ -2048,26 +2203,31 @@ def create_excel(
|
|
| 2048 |
)
|
| 2049 |
|
| 2050 |
|
| 2051 |
-
#
|
| 2052 |
-
#
|
| 2053 |
-
#
|
| 2054 |
|
| 2055 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2056 |
|
| 2057 |
face_positions = (
|
| 2058 |
-
|
| 2059 |
pd.crosstab(
|
| 2060 |
faces_df["horizontal_position"],
|
| 2061 |
faces_df["vertical_position"]
|
| 2062 |
)
|
| 2063 |
-
|
| 2064 |
.reset_index()
|
| 2065 |
-
|
| 2066 |
)
|
| 2067 |
|
| 2068 |
else:
|
| 2069 |
|
| 2070 |
-
face_positions = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2071 |
|
| 2072 |
|
| 2073 |
face_positions.to_excel(
|
|
@@ -2077,26 +2237,31 @@ def create_excel(
|
|
| 2077 |
)
|
| 2078 |
|
| 2079 |
|
| 2080 |
-
#
|
| 2081 |
-
#
|
| 2082 |
-
#
|
| 2083 |
|
| 2084 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2085 |
|
| 2086 |
gender_by_category = (
|
| 2087 |
-
|
| 2088 |
pd.crosstab(
|
| 2089 |
faces_df["category"],
|
| 2090 |
faces_df["auto_perceived_gender"]
|
| 2091 |
)
|
| 2092 |
-
|
| 2093 |
.reset_index()
|
| 2094 |
-
|
| 2095 |
)
|
| 2096 |
|
| 2097 |
else:
|
| 2098 |
|
| 2099 |
-
gender_by_category = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2100 |
|
| 2101 |
|
| 2102 |
gender_by_category.to_excel(
|
|
@@ -2106,72 +2271,75 @@ def create_excel(
|
|
| 2106 |
)
|
| 2107 |
|
| 2108 |
|
| 2109 |
-
#
|
| 2110 |
-
#
|
| 2111 |
-
#
|
| 2112 |
|
| 2113 |
-
if
|
|
|
|
|
|
|
|
|
|
| 2114 |
|
| 2115 |
category_summary = (
|
| 2116 |
-
|
| 2117 |
images_df
|
| 2118 |
.groupby("category")
|
| 2119 |
.agg(
|
| 2120 |
-
|
| 2121 |
images=(
|
| 2122 |
"filename",
|
| 2123 |
"count"
|
| 2124 |
),
|
| 2125 |
-
|
| 2126 |
total_objects=(
|
| 2127 |
"objects_detected",
|
| 2128 |
"sum"
|
| 2129 |
),
|
| 2130 |
-
|
| 2131 |
mean_objects=(
|
| 2132 |
"objects_detected",
|
| 2133 |
"mean"
|
| 2134 |
),
|
| 2135 |
-
|
| 2136 |
total_faces=(
|
| 2137 |
"faces_detected",
|
| 2138 |
"sum"
|
| 2139 |
),
|
| 2140 |
-
|
| 2141 |
mean_faces=(
|
| 2142 |
"faces_detected",
|
| 2143 |
"mean"
|
| 2144 |
),
|
| 2145 |
-
|
| 2146 |
auto_female_faces=(
|
| 2147 |
"auto_female_faces",
|
| 2148 |
"sum"
|
| 2149 |
),
|
| 2150 |
-
|
| 2151 |
auto_male_faces=(
|
| 2152 |
"auto_male_faces",
|
| 2153 |
"sum"
|
| 2154 |
),
|
| 2155 |
-
|
| 2156 |
auto_uncertain_faces=(
|
| 2157 |
"auto_uncertain_faces",
|
| 2158 |
"sum"
|
| 2159 |
),
|
| 2160 |
-
|
| 2161 |
mean_processing_seconds=(
|
| 2162 |
"processing_seconds",
|
| 2163 |
"mean"
|
| 2164 |
),
|
| 2165 |
-
|
| 2166 |
)
|
| 2167 |
-
|
| 2168 |
.reset_index()
|
| 2169 |
-
|
| 2170 |
)
|
| 2171 |
|
| 2172 |
else:
|
| 2173 |
|
| 2174 |
-
category_summary = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2175 |
|
| 2176 |
|
| 2177 |
category_summary.to_excel(
|
|
@@ -2181,14 +2349,20 @@ def create_excel(
|
|
| 2181 |
)
|
| 2182 |
|
| 2183 |
|
| 2184 |
-
#
|
| 2185 |
-
#
|
| 2186 |
-
#
|
| 2187 |
|
| 2188 |
if not faces_df.empty:
|
| 2189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2190 |
manual_coding = faces_df[
|
| 2191 |
-
|
| 2192 |
].copy()
|
| 2193 |
|
| 2194 |
else:
|
|
@@ -2205,154 +2379,67 @@ def create_excel(
|
|
| 2205 |
)
|
| 2206 |
|
| 2207 |
|
| 2208 |
-
#
|
| 2209 |
-
#
|
| 2210 |
-
#
|
| 2211 |
-
|
| 2212 |
-
summary = pd.DataFrame({
|
| 2213 |
-
|
| 2214 |
-
"metric": [
|
| 2215 |
-
|
| 2216 |
-
"Images",
|
| 2217 |
-
|
| 2218 |
-
"Object detections",
|
| 2219 |
-
|
| 2220 |
-
"Unique object classes",
|
| 2221 |
-
|
| 2222 |
-
"Face detections",
|
| 2223 |
-
|
| 2224 |
-
"Faces auto-labelled female",
|
| 2225 |
|
| 2226 |
-
|
| 2227 |
|
| 2228 |
-
"Faces auto-labelled uncertain",
|
| 2229 |
|
| 2230 |
-
|
|
|
|
| 2231 |
|
| 2232 |
-
|
| 2233 |
-
|
| 2234 |
-
"Average faces/image",
|
| 2235 |
-
|
| 2236 |
-
"Average face area (%)",
|
| 2237 |
-
|
| 2238 |
-
],
|
| 2239 |
-
|
| 2240 |
-
"value": [
|
| 2241 |
-
|
| 2242 |
-
len(images_df),
|
| 2243 |
-
|
| 2244 |
-
len(objects_df),
|
| 2245 |
-
|
| 2246 |
-
(
|
| 2247 |
-
objects_df["object"].nunique()
|
| 2248 |
-
if not objects_df.empty
|
| 2249 |
-
else 0
|
| 2250 |
-
),
|
| 2251 |
|
| 2252 |
-
|
| 2253 |
|
| 2254 |
-
|
| 2255 |
-
|
| 2256 |
-
|
| 2257 |
-
|
| 2258 |
-
|
| 2259 |
-
]
|
| 2260 |
-
== "female"
|
| 2261 |
-
).sum()
|
| 2262 |
-
)
|
| 2263 |
-
|
| 2264 |
-
if not faces_df.empty
|
| 2265 |
-
|
| 2266 |
-
else 0
|
| 2267 |
-
),
|
| 2268 |
-
|
| 2269 |
-
(
|
| 2270 |
-
int(
|
| 2271 |
-
(
|
| 2272 |
-
faces_df[
|
| 2273 |
-
"auto_perceived_gender"
|
| 2274 |
-
]
|
| 2275 |
-
== "male"
|
| 2276 |
-
).sum()
|
| 2277 |
-
)
|
| 2278 |
-
|
| 2279 |
-
if not faces_df.empty
|
| 2280 |
-
|
| 2281 |
-
else 0
|
| 2282 |
-
),
|
| 2283 |
-
|
| 2284 |
-
(
|
| 2285 |
-
int(
|
| 2286 |
-
(
|
| 2287 |
-
faces_df[
|
| 2288 |
-
"auto_perceived_gender"
|
| 2289 |
-
]
|
| 2290 |
-
== "uncertain"
|
| 2291 |
-
).sum()
|
| 2292 |
-
)
|
| 2293 |
-
|
| 2294 |
-
if not faces_df.empty
|
| 2295 |
-
|
| 2296 |
-
else 0
|
| 2297 |
-
),
|
| 2298 |
-
|
| 2299 |
-
(
|
| 2300 |
-
images_df[
|
| 2301 |
-
"category"
|
| 2302 |
-
].nunique()
|
| 2303 |
-
|
| 2304 |
-
if not images_df.empty
|
| 2305 |
|
| 2306 |
-
else 0
|
| 2307 |
-
),
|
| 2308 |
|
| 2309 |
-
|
| 2310 |
-
|
| 2311 |
-
"objects_detected"
|
| 2312 |
-
].mean()
|
| 2313 |
|
| 2314 |
-
|
| 2315 |
|
| 2316 |
-
|
| 2317 |
-
),
|
| 2318 |
|
| 2319 |
-
(
|
| 2320 |
-
|
| 2321 |
-
|
| 2322 |
-
].mean()
|
| 2323 |
|
| 2324 |
-
|
| 2325 |
|
| 2326 |
-
|
| 2327 |
-
),
|
| 2328 |
|
| 2329 |
-
|
| 2330 |
-
|
| 2331 |
-
|
| 2332 |
-
].mean() * 100
|
| 2333 |
|
| 2334 |
-
|
| 2335 |
|
| 2336 |
-
|
| 2337 |
-
),
|
| 2338 |
|
| 2339 |
-
|
| 2340 |
|
| 2341 |
-
|
| 2342 |
|
| 2343 |
|
| 2344 |
-
|
| 2345 |
-
|
| 2346 |
-
|
| 2347 |
-
|
| 2348 |
-
|
|
|
|
| 2349 |
|
| 2350 |
|
| 2351 |
output.seek(0)
|
| 2352 |
|
| 2353 |
return output
|
| 2354 |
|
| 2355 |
-
|
| 2356 |
# ============================================================
|
| 2357 |
# MAIN ZIP ANALYSIS
|
| 2358 |
# ============================================================
|
|
|
|
| 1907 |
objects_df,
|
| 1908 |
faces_df
|
| 1909 |
):
|
| 1910 |
+
"""
|
| 1911 |
+
Create a robust Excel workbook.
|
| 1912 |
|
| 1913 |
+
Always creates a visible Summary sheet first,
|
| 1914 |
+
so openpyxl never encounters a workbook with
|
| 1915 |
+
zero visible worksheets.
|
| 1916 |
+
"""
|
| 1917 |
|
| 1918 |
+
output = io.BytesIO()
|
| 1919 |
|
| 1920 |
with pd.ExcelWriter(
|
| 1921 |
output,
|
| 1922 |
engine="openpyxl"
|
| 1923 |
+
) as writer:
|
| 1924 |
+
|
| 1925 |
+
# ====================================================
|
| 1926 |
+
# SUMMARY — ALWAYS CREATED FIRST
|
| 1927 |
+
# ====================================================
|
| 1928 |
+
|
| 1929 |
+
summary_rows = [
|
| 1930 |
+
{
|
| 1931 |
+
"metric": "Images",
|
| 1932 |
+
"value": len(images_df)
|
| 1933 |
+
},
|
| 1934 |
+
{
|
| 1935 |
+
"metric": "Object detections",
|
| 1936 |
+
"value": len(objects_df)
|
| 1937 |
+
},
|
| 1938 |
+
{
|
| 1939 |
+
"metric": "Unique object classes",
|
| 1940 |
+
"value": (
|
| 1941 |
+
objects_df["object"].nunique()
|
| 1942 |
+
if (
|
| 1943 |
+
not objects_df.empty
|
| 1944 |
+
and "object" in objects_df.columns
|
| 1945 |
+
)
|
| 1946 |
+
else 0
|
| 1947 |
+
)
|
| 1948 |
+
},
|
| 1949 |
+
{
|
| 1950 |
+
"metric": "Face detections",
|
| 1951 |
+
"value": len(faces_df)
|
| 1952 |
+
},
|
| 1953 |
+
{
|
| 1954 |
+
"metric": "Categories",
|
| 1955 |
+
"value": (
|
| 1956 |
+
images_df["category"].nunique()
|
| 1957 |
+
if (
|
| 1958 |
+
not images_df.empty
|
| 1959 |
+
and "category" in images_df.columns
|
| 1960 |
+
)
|
| 1961 |
+
else 0
|
| 1962 |
+
)
|
| 1963 |
+
},
|
| 1964 |
+
{
|
| 1965 |
+
"metric": "Average objects/image",
|
| 1966 |
+
"value": (
|
| 1967 |
+
images_df["objects_detected"].mean()
|
| 1968 |
+
if (
|
| 1969 |
+
not images_df.empty
|
| 1970 |
+
and "objects_detected" in images_df.columns
|
| 1971 |
+
)
|
| 1972 |
+
else 0
|
| 1973 |
+
)
|
| 1974 |
+
},
|
| 1975 |
+
{
|
| 1976 |
+
"metric": "Average faces/image",
|
| 1977 |
+
"value": (
|
| 1978 |
+
images_df["faces_detected"].mean()
|
| 1979 |
+
if (
|
| 1980 |
+
not images_df.empty
|
| 1981 |
+
and "faces_detected" in images_df.columns
|
| 1982 |
+
)
|
| 1983 |
+
else 0
|
| 1984 |
+
)
|
| 1985 |
+
},
|
| 1986 |
+
{
|
| 1987 |
+
"metric": "Average face area (%)",
|
| 1988 |
+
"value": (
|
| 1989 |
+
faces_df["area_fraction"].mean() * 100
|
| 1990 |
+
if (
|
| 1991 |
+
not faces_df.empty
|
| 1992 |
+
and "area_fraction" in faces_df.columns
|
| 1993 |
+
)
|
| 1994 |
+
else 0
|
| 1995 |
+
)
|
| 1996 |
+
},
|
| 1997 |
+
]
|
| 1998 |
|
| 1999 |
+
summary_df = pd.DataFrame(
|
| 2000 |
+
summary_rows
|
| 2001 |
+
)
|
| 2002 |
|
| 2003 |
+
summary_df.to_excel(
|
| 2004 |
+
writer,
|
| 2005 |
+
sheet_name="Summary",
|
| 2006 |
+
index=False
|
| 2007 |
+
)
|
| 2008 |
+
|
| 2009 |
+
|
| 2010 |
+
# ====================================================
|
| 2011 |
+
# IMAGES
|
| 2012 |
+
# ====================================================
|
| 2013 |
+
|
| 2014 |
+
if images_df.empty:
|
| 2015 |
+
|
| 2016 |
+
images_export = pd.DataFrame(
|
| 2017 |
+
columns=[
|
| 2018 |
+
"filename",
|
| 2019 |
+
"relative_path",
|
| 2020 |
+
"category",
|
| 2021 |
+
"subcategory",
|
| 2022 |
+
"width",
|
| 2023 |
+
"height",
|
| 2024 |
+
"objects_detected",
|
| 2025 |
+
"faces_detected",
|
| 2026 |
+
"auto_female_faces",
|
| 2027 |
+
"auto_male_faces",
|
| 2028 |
+
"auto_uncertain_faces",
|
| 2029 |
+
"object_types",
|
| 2030 |
+
"processing_seconds",
|
| 2031 |
+
]
|
| 2032 |
+
)
|
| 2033 |
+
|
| 2034 |
+
else:
|
| 2035 |
|
| 2036 |
+
images_export = images_df.copy()
|
| 2037 |
+
|
| 2038 |
+
|
| 2039 |
+
images_export.to_excel(
|
| 2040 |
writer,
|
| 2041 |
sheet_name="Images",
|
| 2042 |
index=False
|
| 2043 |
)
|
| 2044 |
|
| 2045 |
+
|
| 2046 |
+
# ====================================================
|
| 2047 |
+
# OBJECTS
|
| 2048 |
+
# ====================================================
|
| 2049 |
+
|
| 2050 |
+
if objects_df.empty:
|
| 2051 |
+
|
| 2052 |
+
objects_export = pd.DataFrame(
|
| 2053 |
+
columns=OBJECT_COLUMNS
|
| 2054 |
+
)
|
| 2055 |
+
|
| 2056 |
+
else:
|
| 2057 |
+
|
| 2058 |
+
objects_export = objects_df.copy()
|
| 2059 |
+
|
| 2060 |
+
|
| 2061 |
+
objects_export.to_excel(
|
| 2062 |
writer,
|
| 2063 |
sheet_name="Objects",
|
| 2064 |
index=False
|
| 2065 |
)
|
| 2066 |
|
| 2067 |
+
|
| 2068 |
+
# ====================================================
|
| 2069 |
+
# FACES
|
| 2070 |
+
# ====================================================
|
| 2071 |
+
|
| 2072 |
+
if faces_df.empty:
|
| 2073 |
+
|
| 2074 |
+
faces_export = pd.DataFrame(
|
| 2075 |
+
columns=FACE_COLUMNS_ALL
|
| 2076 |
+
)
|
| 2077 |
+
|
| 2078 |
+
else:
|
| 2079 |
+
|
| 2080 |
+
faces_export = faces_df.copy()
|
| 2081 |
+
|
| 2082 |
+
|
| 2083 |
+
faces_export.to_excel(
|
| 2084 |
writer,
|
| 2085 |
sheet_name="Faces",
|
| 2086 |
index=False
|
| 2087 |
)
|
| 2088 |
|
| 2089 |
|
| 2090 |
+
# ====================================================
|
| 2091 |
+
# OBJECT SUMMARY
|
| 2092 |
+
# ====================================================
|
| 2093 |
|
| 2094 |
if not objects_df.empty:
|
| 2095 |
|
| 2096 |
object_summary = (
|
|
|
|
| 2097 |
objects_df
|
| 2098 |
.groupby("object")
|
| 2099 |
.agg(
|
|
|
|
| 2100 |
detections=(
|
| 2101 |
"object",
|
| 2102 |
"size"
|
| 2103 |
),
|
|
|
|
| 2104 |
mean_confidence=(
|
| 2105 |
"confidence",
|
| 2106 |
"mean"
|
| 2107 |
),
|
|
|
|
| 2108 |
mean_area_fraction=(
|
| 2109 |
"area_fraction",
|
| 2110 |
"mean"
|
| 2111 |
),
|
|
|
|
| 2112 |
)
|
|
|
|
| 2113 |
.sort_values(
|
| 2114 |
"detections",
|
| 2115 |
ascending=False
|
| 2116 |
)
|
|
|
|
| 2117 |
.reset_index()
|
|
|
|
| 2118 |
)
|
| 2119 |
|
| 2120 |
else:
|
| 2121 |
|
| 2122 |
+
object_summary = pd.DataFrame(
|
| 2123 |
+
columns=[
|
| 2124 |
+
"object",
|
| 2125 |
+
"detections",
|
| 2126 |
+
"mean_confidence",
|
| 2127 |
+
"mean_area_fraction",
|
| 2128 |
+
]
|
| 2129 |
+
)
|
| 2130 |
|
| 2131 |
|
| 2132 |
object_summary.to_excel(
|
|
|
|
| 2136 |
)
|
| 2137 |
|
| 2138 |
|
| 2139 |
+
# ====================================================
|
| 2140 |
+
# OBJECTS BY CATEGORY
|
| 2141 |
+
# ====================================================
|
| 2142 |
|
| 2143 |
+
if (
|
| 2144 |
+
not objects_df.empty
|
| 2145 |
+
and "category" in objects_df.columns
|
| 2146 |
+
and "object" in objects_df.columns
|
| 2147 |
+
):
|
| 2148 |
|
| 2149 |
objects_by_category = (
|
|
|
|
| 2150 |
pd.crosstab(
|
| 2151 |
objects_df["category"],
|
| 2152 |
objects_df["object"]
|
| 2153 |
)
|
|
|
|
| 2154 |
.reset_index()
|
|
|
|
| 2155 |
)
|
| 2156 |
|
| 2157 |
else:
|
| 2158 |
|
| 2159 |
+
objects_by_category = pd.DataFrame(
|
| 2160 |
+
columns=["category"]
|
| 2161 |
+
)
|
| 2162 |
|
| 2163 |
|
| 2164 |
objects_by_category.to_excel(
|
|
|
|
| 2168 |
)
|
| 2169 |
|
| 2170 |
|
| 2171 |
+
# ====================================================
|
| 2172 |
+
# FACES BY CATEGORY
|
| 2173 |
+
# ====================================================
|
| 2174 |
|
| 2175 |
+
if (
|
| 2176 |
+
not faces_df.empty
|
| 2177 |
+
and "category" in faces_df.columns
|
| 2178 |
+
):
|
| 2179 |
|
| 2180 |
faces_by_category = (
|
|
|
|
| 2181 |
faces_df
|
| 2182 |
.groupby("category")
|
| 2183 |
.size()
|
| 2184 |
.reset_index(
|
| 2185 |
name="detected_faces"
|
| 2186 |
)
|
|
|
|
| 2187 |
)
|
| 2188 |
|
| 2189 |
else:
|
| 2190 |
|
| 2191 |
+
faces_by_category = pd.DataFrame(
|
| 2192 |
+
columns=[
|
| 2193 |
+
"category",
|
| 2194 |
+
"detected_faces"
|
| 2195 |
+
]
|
| 2196 |
+
)
|
| 2197 |
|
| 2198 |
|
| 2199 |
faces_by_category.to_excel(
|
|
|
|
| 2203 |
)
|
| 2204 |
|
| 2205 |
|
| 2206 |
+
# ====================================================
|
| 2207 |
+
# FACE POSITIONS
|
| 2208 |
+
# ====================================================
|
| 2209 |
|
| 2210 |
+
if (
|
| 2211 |
+
not faces_df.empty
|
| 2212 |
+
and "horizontal_position" in faces_df.columns
|
| 2213 |
+
and "vertical_position" in faces_df.columns
|
| 2214 |
+
):
|
| 2215 |
|
| 2216 |
face_positions = (
|
|
|
|
| 2217 |
pd.crosstab(
|
| 2218 |
faces_df["horizontal_position"],
|
| 2219 |
faces_df["vertical_position"]
|
| 2220 |
)
|
|
|
|
| 2221 |
.reset_index()
|
|
|
|
| 2222 |
)
|
| 2223 |
|
| 2224 |
else:
|
| 2225 |
|
| 2226 |
+
face_positions = pd.DataFrame(
|
| 2227 |
+
columns=[
|
| 2228 |
+
"horizontal_position"
|
| 2229 |
+
]
|
| 2230 |
+
)
|
| 2231 |
|
| 2232 |
|
| 2233 |
face_positions.to_excel(
|
|
|
|
| 2237 |
)
|
| 2238 |
|
| 2239 |
|
| 2240 |
+
# ====================================================
|
| 2241 |
+
# AUTOMATIC PERCEIVED GENDER
|
| 2242 |
+
# ====================================================
|
| 2243 |
|
| 2244 |
+
if (
|
| 2245 |
+
not faces_df.empty
|
| 2246 |
+
and "category" in faces_df.columns
|
| 2247 |
+
and "auto_perceived_gender" in faces_df.columns
|
| 2248 |
+
):
|
| 2249 |
|
| 2250 |
gender_by_category = (
|
|
|
|
| 2251 |
pd.crosstab(
|
| 2252 |
faces_df["category"],
|
| 2253 |
faces_df["auto_perceived_gender"]
|
| 2254 |
)
|
|
|
|
| 2255 |
.reset_index()
|
|
|
|
| 2256 |
)
|
| 2257 |
|
| 2258 |
else:
|
| 2259 |
|
| 2260 |
+
gender_by_category = pd.DataFrame(
|
| 2261 |
+
columns=[
|
| 2262 |
+
"category"
|
| 2263 |
+
]
|
| 2264 |
+
)
|
| 2265 |
|
| 2266 |
|
| 2267 |
gender_by_category.to_excel(
|
|
|
|
| 2271 |
)
|
| 2272 |
|
| 2273 |
|
| 2274 |
+
# ====================================================
|
| 2275 |
+
# CATEGORY SUMMARY
|
| 2276 |
+
# ====================================================
|
| 2277 |
|
| 2278 |
+
if (
|
| 2279 |
+
not images_df.empty
|
| 2280 |
+
and "category" in images_df.columns
|
| 2281 |
+
):
|
| 2282 |
|
| 2283 |
category_summary = (
|
|
|
|
| 2284 |
images_df
|
| 2285 |
.groupby("category")
|
| 2286 |
.agg(
|
|
|
|
| 2287 |
images=(
|
| 2288 |
"filename",
|
| 2289 |
"count"
|
| 2290 |
),
|
|
|
|
| 2291 |
total_objects=(
|
| 2292 |
"objects_detected",
|
| 2293 |
"sum"
|
| 2294 |
),
|
|
|
|
| 2295 |
mean_objects=(
|
| 2296 |
"objects_detected",
|
| 2297 |
"mean"
|
| 2298 |
),
|
|
|
|
| 2299 |
total_faces=(
|
| 2300 |
"faces_detected",
|
| 2301 |
"sum"
|
| 2302 |
),
|
|
|
|
| 2303 |
mean_faces=(
|
| 2304 |
"faces_detected",
|
| 2305 |
"mean"
|
| 2306 |
),
|
|
|
|
| 2307 |
auto_female_faces=(
|
| 2308 |
"auto_female_faces",
|
| 2309 |
"sum"
|
| 2310 |
),
|
|
|
|
| 2311 |
auto_male_faces=(
|
| 2312 |
"auto_male_faces",
|
| 2313 |
"sum"
|
| 2314 |
),
|
|
|
|
| 2315 |
auto_uncertain_faces=(
|
| 2316 |
"auto_uncertain_faces",
|
| 2317 |
"sum"
|
| 2318 |
),
|
|
|
|
| 2319 |
mean_processing_seconds=(
|
| 2320 |
"processing_seconds",
|
| 2321 |
"mean"
|
| 2322 |
),
|
|
|
|
| 2323 |
)
|
|
|
|
| 2324 |
.reset_index()
|
|
|
|
| 2325 |
)
|
| 2326 |
|
| 2327 |
else:
|
| 2328 |
|
| 2329 |
+
category_summary = pd.DataFrame(
|
| 2330 |
+
columns=[
|
| 2331 |
+
"category",
|
| 2332 |
+
"images",
|
| 2333 |
+
"total_objects",
|
| 2334 |
+
"mean_objects",
|
| 2335 |
+
"total_faces",
|
| 2336 |
+
"mean_faces",
|
| 2337 |
+
"auto_female_faces",
|
| 2338 |
+
"auto_male_faces",
|
| 2339 |
+
"auto_uncertain_faces",
|
| 2340 |
+
"mean_processing_seconds",
|
| 2341 |
+
]
|
| 2342 |
+
)
|
| 2343 |
|
| 2344 |
|
| 2345 |
category_summary.to_excel(
|
|
|
|
| 2349 |
)
|
| 2350 |
|
| 2351 |
|
| 2352 |
+
# ====================================================
|
| 2353 |
+
# MANUAL CODING
|
| 2354 |
+
# ====================================================
|
| 2355 |
|
| 2356 |
if not faces_df.empty:
|
| 2357 |
|
| 2358 |
+
available_manual_columns = [
|
| 2359 |
+
col
|
| 2360 |
+
for col in FACE_COLUMNS_MANUAL
|
| 2361 |
+
if col in faces_df.columns
|
| 2362 |
+
]
|
| 2363 |
+
|
| 2364 |
manual_coding = faces_df[
|
| 2365 |
+
available_manual_columns
|
| 2366 |
].copy()
|
| 2367 |
|
| 2368 |
else:
|
|
|
|
| 2379 |
)
|
| 2380 |
|
| 2381 |
|
| 2382 |
+
# ====================================================
|
| 2383 |
+
# FORMAT WORKBOOK
|
| 2384 |
+
# ====================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2385 |
|
| 2386 |
+
workbook = writer.book
|
| 2387 |
|
|
|
|
| 2388 |
|
| 2389 |
+
# Make sure Summary is active and visible
|
| 2390 |
+
if "Summary" in workbook.sheetnames:
|
| 2391 |
|
| 2392 |
+
summary_sheet = workbook["Summary"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2393 |
|
| 2394 |
+
summary_sheet.sheet_state = "visible"
|
| 2395 |
|
| 2396 |
+
workbook.active = (
|
| 2397 |
+
workbook.sheetnames.index(
|
| 2398 |
+
"Summary"
|
| 2399 |
+
)
|
| 2400 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2401 |
|
|
|
|
|
|
|
| 2402 |
|
| 2403 |
+
# Auto-adjust column widths
|
| 2404 |
+
for worksheet in workbook.worksheets:
|
|
|
|
|
|
|
| 2405 |
|
| 2406 |
+
for column_cells in worksheet.columns:
|
| 2407 |
|
| 2408 |
+
max_length = 0
|
|
|
|
| 2409 |
|
| 2410 |
+
column_letter = (
|
| 2411 |
+
column_cells[0].column_letter
|
| 2412 |
+
)
|
|
|
|
| 2413 |
|
| 2414 |
+
for cell in column_cells:
|
| 2415 |
|
| 2416 |
+
try:
|
|
|
|
| 2417 |
|
| 2418 |
+
cell_length = len(
|
| 2419 |
+
str(cell.value)
|
| 2420 |
+
)
|
|
|
|
| 2421 |
|
| 2422 |
+
if cell_length > max_length:
|
| 2423 |
|
| 2424 |
+
max_length = cell_length
|
|
|
|
| 2425 |
|
| 2426 |
+
except Exception:
|
| 2427 |
|
| 2428 |
+
pass
|
| 2429 |
|
| 2430 |
|
| 2431 |
+
worksheet.column_dimensions[
|
| 2432 |
+
column_letter
|
| 2433 |
+
].width = min(
|
| 2434 |
+
max_length + 2,
|
| 2435 |
+
50
|
| 2436 |
+
)
|
| 2437 |
|
| 2438 |
|
| 2439 |
output.seek(0)
|
| 2440 |
|
| 2441 |
return output
|
| 2442 |
|
|
|
|
| 2443 |
# ============================================================
|
| 2444 |
# MAIN ZIP ANALYSIS
|
| 2445 |
# ============================================================
|