File size: 103,487 Bytes
9a70318 c6191da 650a9c1 af8e8db f6176b5 47f75cd 9a70318 1fe4554 9a70318 e3dadd3 c69ebec 4723b2b 9a70318 4a83cc4 dff08f7 9a70318 650a9c1 7408fc3 9a70318 faad7d4 650a9c1 1fe4554 650a9c1 cb4adb5 650a9c1 af8e8db faad7d4 650a9c1 1fe4554 650a9c1 af8e8db faad7d4 650a9c1 1fe4554 faad7d4 1fe4554 7d35e03 650a9c1 1fe4554 650a9c1 1fe4554 faad7d4 1fe4554 c6c5a0f 1fe4554 650a9c1 ac1a3be be56025 faad7d4 1fe4554 a9a0e92 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 1fe4554 650a9c1 7408fc3 33d55e6 1fe4554 9a70318 7408fc3 9a70318 1fe4554 33d55e6 7408fc3 1fe4554 7408fc3 33d55e6 7408fc3 33d55e6 7408fc3 1fe4554 9a70318 cb4adb5 6a2da42 cb4adb5 c69ebec cb4adb5 9e65eab 9b45c63 f79ec76 9b45c63 cb4adb5 87dbb73 cb4adb5 9b45c63 cb4adb5 8ab1d66 cb4adb5 8ab1d66 9b45c63 cb4adb5 87dbb73 8ab1d66 cb4adb5 8ab1d66 cb4adb5 8ab1d66 9a1b19b 9b45c63 2a29b72 d4cdce6 2a29b72 cb4adb5 f56acef 789efa6 d4cdce6 623166f cb4adb5 789efa6 9b45c63 cb4adb5 9b45c63 cb4adb5 c4ecf92 41351c1 ba7ced0 27f27a1 41351c1 27f27a1 41351c1 27f27a1 41351c1 27f27a1 41351c1 27f27a1 51fd7c4 27f27a1 41351c1 27f27a1 41351c1 27f27a1 51fd7c4 41351c1 51fd7c4 034235d 2064706 41351c1 2064706 27f27a1 2064706 2cc6106 41351c1 034235d 27f27a1 034235d 27f27a1 034235d 27f27a1 41351c1 27f27a1 41351c1 27f27a1 41351c1 37d5cfe 5d70085 37d5cfe 5d70085 37d5cfe 41351c1 16d2aa0 5d70085 41351c1 5d70085 41351c1 16d2aa0 27f27a1 5d70085 41351c1 16d2aa0 27f27a1 16d2aa0 5d70085 41351c1 16d2aa0 41351c1 27f27a1 41351c1 16d2aa0 41351c1 16d2aa0 c2c9951 be56025 16d2aa0 41351c1 16d2aa0 41351c1 16d2aa0 41351c1 237f1c1 27f27a1 9a70318 1fe4554 9a70318 7408fc3 9a70318 7408fc3 9a70318 7d35e03 bdac8dc 7d35e03 a445173 07ebae2 7d35e03 1fe4554 7d35e03 1fe4554 7d35e03 1fe4554 7d35e03 c6191da 1fe4554 7d35e03 1fe4554 650a9c1 a445173 650a9c1 1fe4554 af8e8db 1fe4554 cb0a5f1 faad7d4 a445173 da5dad4 b9384df 1fe4554 b9384df af8e8db 1fe4554 af8e8db 650a9c1 1fe4554 a445173 1fe4554 af8e8db 1fe4554 7d35e03 1fe4554 af8e8db 1fe4554 a445173 1fe4554 650a9c1 7d35e03 1fe4554 7d35e03 650a9c1 1fe4554 9ad17b9 1fe4554 bdac8dc 15fb34b bdac8dc 4a83cc4 bdac8dc 4a83cc4 bdac8dc 4ec52c9 4f64838 a445173 15fb34b 9a70318 1fe4554 9a70318 1fe4554 9a70318 1fe4554 9a70318 1fe4554 9a70318 1fe4554 9d48201 1fe4554 9d48201 1fe4554 9d48201 1fe4554 9a70318 a445173 9a70318 33d55e6 2c3a509 1fe4554 2c3a509 33d55e6 7408fc3 33d55e6 7408fc3 33d55e6 7408fc3 33d55e6 1fe4554 33d55e6 2c3a509 a445173 7408fc3 1fe4554 7408fc3 1fe4554 7408fc3 1fe4554 7408fc3 2c3a509 1fe4554 2c3a509 33d55e6 2c3a509 7408fc3 2c3a509 33d55e6 a445173 1fe4554 6fccf31 1fe4554 7408fc3 2c3a509 7408fc3 1fe4554 2c3a509 7408fc3 2c3a509 1fe4554 a445173 2c3a509 7408fc3 1cb6765 e4b4d17 1cb6765 e4b4d17 1cb6765 47737f2 1cb6765 ae7e31f 33d55e6 ae7e31f 33d55e6 47f75cd 33d55e6 9a70318 a9e4578 9a70318 ae7e31f e4b4d17 33d55e6 e4b4d17 33d55e6 a9e4578 b8fcada cb4adb5 b8fcada 33d55e6 b8fcada 406fb15 47737f2 406fb15 267cb12 d8f440b 267cb12 d8f440b 267cb12 d8f440b 267cb12 d8f440b 267cb12 d8f440b 267cb12 d8f440b f4ed114 267cb12 f4ed114 d8f440b f4ed114 267cb12 f4ed114 e640267 f4ed114 47737f2 a445173 47737f2 ec376c4 47737f2 ec376c4 47737f2 ec376c4 406fb15 47f75cd 6267596 afeb9ad de92eb0 afeb9ad 5a622ab afeb9ad 33d55e6 befe750 33d55e6 de92eb0 cc5b7c3 bb5bc6c 086e54f cc5b7c3 bb5bc6c 9840163 4ffbba7 6df037d 4ffbba7 086e54f 436513f 086e54f 4ffbba7 086e54f e484420 e44745e 0549024 e484420 086e54f ae22ca7 e44745e ae22ca7 c61b6b1 ecbdc28 c61b6b1 e484420 3b7d5ac 765e253 e5b198d 976ea08 5b6e537 569f6e7 5b6e537 f1a18d9 c61b6b1 ae22ca7 f5c19bf e484420 5b6e537 976ea08 569f6e7 ae22ca7 710d7f9 ae22ca7 e484420 f5ca857 5847438 e484420 086e54f 5416f3e 436513f bb5bc6c f5c19bf 710d7f9 f5c19bf 086e54f 436513f 5416f3e 086e54f f5c19bf 4ffbba7 baeac87 4ffbba7 086e54f d543c26 086e54f 08e885d ec376c4 ad0c0a9 14277e6 33d55e6 a5df19b cc5b7c3 d93dd78 08e885d cc5b7c3 002f8e8 6df037d a5df19b 08e885d cc5e951 08e885d 002f8e8 08e885d 002f8e8 444aa66 002f8e8 444aa66 82931b9 444aa66 82931b9 444aa66 82931b9 08e885d 0ed574b 08e885d 82931b9 002f8e8 e48453a 733d6f3 002f8e8 e48453a 733d6f3 002f8e8 733d6f3 e48453a 444aa66 4e95608 e48453a 733d6f3 82931b9 733d6f3 82931b9 733d6f3 444aa66 733d6f3 444aa66 733d6f3 002f8e8 82931b9 08e885d 444aa66 08e885d f1a18d9 a20d236 cac4c24 a20d236 fe49660 a20d236 fe49660 a20d236 26bdf85 a20d236 35b7342 a20d236 5b88fe2 ef8991a 5b88fe2 406a651 ef8991a 5b88fe2 ee39494 5b88fe2 2ce75af 5b88fe2 2ce75af 5b88fe2 4b6f6b5 5b88fe2 4b6f6b5 7c9de90 4b6f6b5 3eae7c0 92a679a a20d236 241259a c54fdb8 a20d236 c54fdb8 a20d236 c54fdb8 6da3d2a a20d236 cb57464 a20d236 fe49660 a20d236 cb57464 37b4d1f a20d236 37b4d1f 9a70318 cb4adb5 47f75cd ca76d8b cb4adb5 ca76d8b 3807b6f 160993b ca76d8b 7dd6fe5 153e917 ca76d8b 6941bcb cb4adb5 42e5831 cb4adb5 160993b 353a9df cb4adb5 4e0c3e1 a445173 cb4adb5 47f75cd cb4adb5 4e0c3e1 ca76d8b 47f75cd cb4adb5 69000b5 47f75cd e640267 9a70318 ae7e31f 47f75cd 9a70318 ae7e31f 456c791 d27fb66 3744e15 cb4adb5 9a70318 33d55e6 9a70318 cb4adb5 9a70318 47f75cd 56d50e6 40e899f 9a70318 cb4adb5 b8fcada 40e899f 42e5831 40e899f 9a70318 40e899f 47f75cd cb4adb5 c8329cf cb4adb5 c8329cf cb4adb5 2259119 47737f2 2259119 cb4adb5 47f75cd cb4adb5 f2e0c7f 4e0c3e1 47f75cd 4e0c3e1 cb4adb5 9a70318 33d55e6 cb4adb5 33d55e6 cb4adb5 9a70318 f2e0c7f 34ff739 f2e0c7f 69000b5 f2e0c7f ae7e31f 9a70318 ae7e31f 9a70318 a9e4578 69000b5 9a70318 47f75cd 1fe4554 c6c5a0f 1fe4554 9a70318 33d55e6 9a70318 1bde93d ba7235c 1bde93d ba7235c db761c8 ba7235c a44e147 1bde93d 6951beb 1bde93d 6951beb 1bde93d 9a70318 650a9c1 9a70318 650a9c1 9a70318 650a9c1 9a70318 e652b80 650a9c1 33d55e6 9a70318 7d35e03 1fe4554 7d35e03 9a70318 7d35e03 9a70318 7d35e03 e652b80 7d35e03 33d55e6 7d35e03 33d55e6 7d35e03 33d55e6 7408fc3 33d55e6 7d35e03 7408fc3 7d35e03 7408fc3 7d35e03 7408fc3 7d35e03 7408fc3 7d35e03 7408fc3 7d35e03 dcf20ee 7d35e03 dcf20ee bae9885 dcf20ee 7d35e03 6c8f9c7 7d35e03 dcf20ee 7d35e03 dcf20ee bae9885 dcf20ee 7d35e03 6c8f9c7 7d35e03 dcf20ee 7d35e03 dcf20ee 7d35e03 6c8f9c7 7d35e03 a445173 7d35e03 a445173 6c8f9c7 7d35e03 33d55e6 7408fc3 7d35e03 33d55e6 7408fc3 33d55e6 7408fc3 cb4adb5 6c8f9c7 9ad17b9 7d35e03 99562c8 7d35e03 99562c8 7d35e03 9ad17b9 99562c8 f2e0c7f 99562c8 f2e0c7f 69000b5 27b2eb3 f2e0c7f 7d35e03 7408fc3 fdddba4 7d35e03 7408fc3 9ad17b9 7d35e03 33d55e6 7d35e03 33d55e6 7d35e03 fdddba4 7d35e03 79b9a95 b470da6 79b9a95 bae9885 79b9a95 b470da6 79b9a95 bae9885 79b9a95 b470da6 79b9a95 bae9885 79b9a95 b470da6 322d3fe b470da6 79b9a95 322d3fe 79b9a95 322d3fe 79b9a95 33d55e6 e652b80 9a70318 7d35e03 33d55e6 7d35e03 33d55e6 7d35e03 69000b5 7d35e03 5bf84ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 | import gradio as gr
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
from diffusers import StableDiffusionInpaintPipeline, AutoencoderKL
from diffusers import DPMSolverMultistepScheduler, PNDMScheduler
from controlnet_module import controlnet_processor
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
import torch
from PIL import Image, ImageDraw
import time
import os
import tempfile
import random
import re
from PIL import ImageFilter # Für GaussianBlur wird nur für SAM benötigt!
import numpy as np
# === OPTIMIERTE EINSTELLUNGEN ===
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if device == "cuda" else torch.float32
IMG_SIZE = 512
MAX_IMAGE_SIZE = 4096 # Maximale Bildgröße für Verarbeitung
print(f"Running on: {device}")
# === MODELLKONFIGURATION (NUR 2 MODELLE) ===
MODEL_CONFIGS = {
"runwayml/stable-diffusion-v1-5": {
"name": "🏠 Stable Diffusion 1.5 (Universal)",
"description": "Universal model, good all-rounder, reliable results",
"requires_vae": False,
"vae_model": "stabilityai/sd-vae-ft-mse",
"recommended_steps": 35,
"recommended_cfg": 7.5,
"supports_fp16": True
},
"SG161222/Realistic_Vision_V6.0_B1_noVAE": {
"name": "👤 Realistic Vision V6.0 (Portraits)",
"description": "Best for photorealistic faces, skin details, human portraits",
"requires_vae": True,
"vae_model": "stabilityai/sd-vae-ft-mse",
"recommended_steps": 40,
"recommended_cfg": 7.0,
"supports_fp16": False
}
}
# === SAFETENSORS KONFIGURATION ===
SAFETENSORS_MODELS = ["runwayml/stable-diffusion-v1-5"]
# Aktuell ausgewähltes Modell (wird vom User gesetzt)
current_model_id = "runwayml/stable-diffusion-v1-5"
# === AUTOMATISCHE NEGATIVE PROMPT GENERIERUNG ===
def auto_negative_prompt(positive_prompt):
"""Generiert automatisch negative Prompts basierend auf dem positiven Prompt"""
p = positive_prompt.lower()
negatives = []
# Personen / Portraits
if any(w in p for w in [
"person", "man", "woman", "face", "portrait", "team", "employee",
"people", "crowd", "character", "figure", "human", "child", "baby",
"girl", "boy", "lady", "gentleman", "fairy", "elf", "dwarf", "santa claus",
"mermaid", "angel", "demon", "witch", "wizard", "creature", "being",
"model", "actor", "actress", "celebrity", "avatar", "group"]):
negatives.append(
"blurry face, lowres face, deformed pupils, bad anatomy, malformed hands, extra fingers, uneven eyes, distorted face, "
"unrealisticy skin, mutated, ugly, disfigured, poorly drawn face, "
"missing limbs, extra limbs, fused fingers, too many fingers, bad teeth, "
"mutated hands, long neck, extra wings, multiple wings,grainy face, noisy face, "
"compression artifacts, rendering artifacts, digital artifacts, overprocessed face, oversmoothed face "
)
# Business / Corporate
if any(w in p for w in ["office", "business", "team", "meeting", "corporate", "company", "workplace"]):
negatives.append(
"overexposed, oversaturated, harsh lighting, watermark, text, logo, brand"
)
# Produkt / CGI
if any(w in p for w in ["product", "packshot", "mockup", "render", "3d", "cgi", "packaging"]):
negatives.append(
"plastic texture, noisy, overly reflective surfaces, watermark, text, low poly"
)
# Landschaft / Umgebung
if any(w in p for w in ["landscape", "nature", "mountain", "forest", "outdoor", "beach", "sky"]):
negatives.append(
"blurry, oversaturated, unnatural colors, distorted horizon, floating objects"
)
# Logos / Symbole
if any(w in p for w in ["logo", "symbol", "icon", "typography", "badge", "emblem"]):
negatives.append(
"watermark, signature, username, text, writing, scribble, messy"
)
# Architektur / Gebäude
if any(w in p for w in ["building", "architecture", "house", "interior", "room", "facade"]):
negatives.append(
"deformed, distorted perspective, floating objects, collapsing structure"
)
# Basis negative Prompts für alle Fälle
base_negatives = "low quality, worst quality, blurry, jpeg artifacts, ugly, deformed"
if negatives:
return base_negatives + ", " + ", ".join(negatives)
else:
return base_negatives
# === HILFSFUNKTION: KOORDINATEN SORTIEREN ===
def sort_coordinates(x1, y1, x2, y2):
"""Sortiert Koordinaten, so dass x1 <= x2 und y1 <= y2"""
sorted_x1 = min(x1, x2)
sorted_x2 = max(x1, x2)
sorted_y1 = min(y1, y2)
sorted_y2 = max(y1, y2)
return sorted_x1, sorted_y1, sorted_x2, sorted_y2
# === GESICHTSMASKEN-FUNKTIONEN (ERWEITERT FÜR 3 MODI) ===
def create_face_mask(image, bbox_coords, mode):
"""
ERWEITERTE FUNKTION: Erzeugt Maske basierend auf 3 Modi
Weiße Bereiche werden VERÄNDERT, Schwarze bleiben ERHALTEN
Parameter:
- image: PIL Image
- bbox_coords: [x1, y1, x2, y2]
- mode: "environment_change", "focus_change", "face_only_change"
Returns:
- PIL Image (L-Modus, 0=schwarz=erhalten, 255=weiß=verändern)
"""
mask = Image.new("L", image.size, 0) # Start mit komplett schwarzer Maske (alles geschützt)
if bbox_coords and all(coord is not None for coord in bbox_coords):
# Sortiere Koordinaten
x1, y1, x2, y2 = sort_coordinates(*bbox_coords)
# Stelle sicher, dass Koordinaten innerhalb des Bildes liegen
x1 = max(0, min(x1, image.width-1))
y1 = max(0, min(y1, image.height-1))
x2 = max(0, min(x2, image.width-1))
y2 = max(0, min(y2, image.height-1))
draw = ImageDraw.Draw(mask)
if mode == "environment_change":
# MODUS 1: Umgebung ändern (Depth + Canny)
# Maske: Alles weiß AUSSER Bereich (schwarz)
draw.rectangle([0, 0, image.size[0], image.size[1]], fill=255) # Alles weiß = verändern
draw.rectangle([x1, y1, x2, y2], fill=0) # Bereich schwarz = geschützt (rechteckig)
print(f"🎯 MODUS: Umgebung ändern - Alles außer BBox wird verändert (BBox: {x1},{y1},{x2},{y2})")
elif mode == "focus_change":
# MODUS 2: Focus verändern (OpenPose + Canny)
# Maske: Nur innerhalb der Box weiß (Rest schwarz)
draw.rectangle([x1, y1, x2, y2], fill=255) # Nur Box weiß = verändern
print(f"🎯 MODUS: Focus verändern - Nur innerhalb der BBox wird verändert (BBox: {x1},{y1},{x2},{y2})")
elif mode == "face_only_change":
# MODUS 3: Ausschließlich Gesicht (Depth + Canny)
# Maske: Nur innerhalb der Box weiß (Rest schwarz) - wie focus_change
draw.rectangle([x1, y1, x2, y2], fill=255) # Nur Box weiß = verändern
print(f"🎯 MODUS: Ausschließlich Gesicht - Nur innerhalb der BBox wird verändert (BBox: {x1},{y1},{x2},{y2})")
return mask
# === KORREKTE GEMEINSAME PROPORTIONALE SKALIERUNG MIT PADDING ===
"""
SKALIERT BILD UND MASKE GEMEINSAM MIT GLEICHEN PROPORTIONEN (MIT PADDING)
Behält das Seitenverhältnis bei und fügt ggf. Padding hinzu
Parameter:
- image: PIL Image (RGB)
- mask: PIL Image (L-Modus, Maske)
- target_size: Zielgröße (Standard 512)
Returns:
- padded_image: skaliertes Bild mit Padding (RGB)
- padded_mask: skalierte Maske mit Padding (L)
- padding_info: Dictionary mit Skalierungsinfo für späteres Compositing
"""
# Herunterskalierung von Bild und BBox/SAM-Maske und SAM-Maske-Binär auf 512x512 für ControlnetInpaint-Pipeline
def scale_image_and_mask_together(image, mask_inpaint, mask_composite, target_size=512, bbox_coords=None, mode=None):
if image is None or mask_inpaint is None or mask_composite is None:
raise ValueError("Bild oder Maske ist None")
if image.size != mask_inpaint.size or image.size != mask_composite.size:
raise ValueError("Bild und Masken haben unterschiedliche Größen: {image.size} vs {mask_inpaint.size}") #Stoppt Programm sofort mit Fehlermeldung!
#Variablen für Bildmaße
original_width, original_height = image.size
# Bestimme Skalierungsfaktor (längere Seite auf target_size)
scale = target_size / max(original_width, original_height)
new_width = int(original_width * scale)
new_height = int(original_height * scale)
print(f"📐 Gemeinsame Skalierung: {original_width}x{original_height} → {new_width}x{new_height} (Skalierung: {scale:.4f})")
# Skaliere Bild und Maske getrennt voneinander aber proportional
scaled_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
scaled_mask_inpaint = mask_inpaint.resize((new_width, new_height), Image.Resampling.NEAREST)
scaled_mask_composite = mask_composite.resize((new_width, new_height), Image.Resampling.NEAREST)
# Auf Zielgröße padden (zentriert)
#Image.new("RGB", (target_size, target_size), (0, 0, 0)) erstellt ein neues, leeres, schwarzes Bild in der Ziel-Verarbeitungsgröße des Modells (512×512 für SD 1.5 oder 1024×1024 für SDXL)
# in das später das Bild eingefügt wird
padded_image = Image.new("RGB", (target_size, target_size), (0, 0, 0))
#Damit wird ein 512x512 Graustufenbild erstellt in das später die BBox eingefügt wird
padded_mask_inpaint = Image.new("L", (target_size, target_size), 0)
padded_mask_composite = Image.new("L", (target_size, target_size), 0)
# Zentrierte Position berechnen
# das ist der Padding-Bereich bei nicht quadratischen 512x512 Bildern damit daraus 512x512-Bilder werden
x_offset = (target_size - new_width) // 2
y_offset = (target_size - new_height) // 2
# mit Hilfe der Offsets kann das skalierte Bild mittig in das RGB-Schwarzbild eingefügt werden. Dadurch ergibt sich
# indirekt der Padding-Bereich.
padded_image.paste(scaled_image, (x_offset, y_offset))
# mit Hilfe der Offsets wird nun die herunterskalierte BBox (entweder als Rechteck oder als SAM-Maske)
# in das Graustufenbild eingefügt. Das Padding ergibt sich aus dem Graustufenbild!
padded_mask_inpaint.paste(scaled_mask_inpaint, (x_offset, y_offset))
padded_mask_composite.paste(scaled_mask_composite, (x_offset, y_offset))
# hiermit wird die (transformierte BBox)= skalierte BBox + Padding berechnet.
scaled_bbox = None
if bbox_coords and all(c is not None for c in bbox_coords):
x1, y1, x2, y2 = bbox_coords
scaled_bbox = (
int(x1 * scale) + x_offset, # Einmalige, konsistente Berechnung
int(y1 * scale) + y_offset,
int(x2 * scale) + x_offset,
int(y2 * scale) + y_offset
)
print(f"📐 Skalierte BBox gespeichert: {scaled_bbox} (von {bbox_coords})")
# WICHTIG: Speichere alle Informationen für späteres Compositing
padding_info = {
'x_offset': x_offset,
'y_offset': y_offset,
'scaled_width': new_width,
'scaled_height': new_height,
'original_width': original_width,
'original_height': original_height,
'scale_factor': scale,
'target_size': target_size,
'original_bbox': bbox_coords,
'scaled_bbox': scaled_bbox,
'mode': mode
}
print(f"📦 Padding hinzugefügt: Offsets ({x_offset}, {y_offset})")
print(f"BBox gespeicher: {bbox_coords}, Modus:{mode}")
print(f"✅ 1 Bild + 2 Masken skaliert. Inpaint-Maske binär: {np.unique(np.array(padded_mask_inpaint))}")
return padded_image, padded_mask_inpaint, padded_mask_composite, padding_info
# Composition Workflow nach Ausgabe ControlnetInpaint-Pipeline
def enhanced_composite_with_sam(original_image, inpaint_result, original_mask,
padding_info, bbox_coords, mode):
"""
COMPOSITING MIT SAM-MASKEN UND BBox-KOORDINATEN
Berücksichtigt die präzisen Kanten der SAM-Maske
"""
print(f"🎨 Verbessertes Compositing für Modus: {mode}")
# Extrahiere Padding-Info
x_offset = padding_info['x_offset']
y_offset = padding_info['y_offset']
scaled_width = padding_info['scaled_width']
scaled_height = padding_info['scaled_height']
scale_factor = padding_info['scale_factor']
original_width = padding_info['original_width']
original_height = padding_info['original_height']
# ==============================================
# FALL 1: Bild war bereits 512×512 (keine Skalierung)
# ==============================================
if scale_factor == 1.0 and x_offset == 0 and y_offset == 0:
print(f"✅ FALL 1: Bild 512×512 - kein Compositing nötig")
return inpaint_result
# ==============================================
# FALL 2 & 3: Bild wurde skaliert
# ==============================================
print(f"🔄 FALL 2/3: Bild skaliert - Compositing mit SAM-Maske")
# 1. PADDING ENTFERNEN von 512×512 Inpaint-Ergebnis
downscaled_result = inpaint_result.crop(
(x_offset, y_offset, x_offset + scaled_width, y_offset + scaled_height)
)
# 2. AUF ORIGINALGRÖßE SKALIEREN
final_image = original_image.copy()
if mode == "environment_change":
# ==============================================
# MODUS: UMWELT ÄNDERN (Objekt bleibt original)
# In dem Fall muß die BBox nicht berücksichtigt werden da Originalbild ausgeschnitten wird
# anhand der SAM-Maske
# ==============================================
print("🌳 Modus: Umwelt ändern mit SAM-Maske")
# Gesamtes bearbeitetes Bild (Ergebnis-Inpaint) hochskalieren
new_background = downscaled_result.resize(
(original_width, original_height),
Image.Resampling.LANCZOS
)
# Originalbild wird kopiert und mit transparenter Folie überzogen (.convert)
# In der Fachsprache heißt das: ein Alpha-Kanal hinzugefügt.
# Diese Folie wird an den Stellen ausgestanzt an denen die Maske schwarz ist.
original_with_alpha = original_image.copy().convert("RGBA")
# Invertierte Maske (BBox, SAM-Maske=original_mask) kommt von SAM zurück!
# Invertierung nötig weil für Alpha-Kanal die Logik andersherum ist. schwarz-weg, weiß-behalten
mask_inverted = Image.eval(original_mask, lambda x: 255 - x)
# Weiche Kanten für natürlichen Übergang, damit werden 1,5 Pixel von Person grau
# und 1,5 Pixel von Umgebung. Effektiv können damit 6-8 Pixel sanft überbrückt werden.
# Gehen graue Pixel nach Inpaint ist das ja ein unsichere Bereich. Inpaint kann Geisterobjekte (halbe Pferde) erzeugen!
soft_mask = mask_inverted.filter(ImageFilter.GaussianBlur(3))
# putalpha stanzt Löcher in die Folie des Originalbildes an denen das Bild weg muß (schwarz),
# läßt Folie ganz da wo weiß (bleibt) und markiert grau für Anpassung. Person bleibt!
original_with_alpha.putalpha(soft_mask)
# Compositing
# Hiermit kommt eine Folie über das neu generierte Bild und wird kopiert
final_image = new_background.copy().convert("RGBA")
# Durch das Einfügen wird die zu erhaltende Person in das neu generierte Bild eingefügt
final_image.paste(original_with_alpha, (0, 0), original_with_alpha)
else:
# ==============================================
# MODUS: FOCUS oder GESICHT ÄNDERN
# Hier muß die BBox berücksichtigt werden da generiertes Bild ausgeschnitten wird
# ohne die BBox wird entlang der SAM-Maske geschnitten -> ungenau!
# ==============================================
mode_name = "Focus" if mode == "focus_change" else "Gesicht"
print(f"👤 Modus: {mode_name} ändern mit SAM-Maske")
if not bbox_coords or not all(c is not None for c in bbox_coords):
# Keine BBox: gesamtes Bild zurückgeben
final_image = downscaled_result.resize(
(original_width, original_height),
Image.Resampling.LANCZOS
)
return final_image.convert("RGB")
# Verwende gespeicherte BBox aus scaled_image_and_mask_together()
if 'scaled_bbox' in padding_info and padding_info['scaled_bbox'] is not None:
bbox_in_512 = padding_info['scaled_bbox'] # ← WICHTIG: Verwende die gespeicherte skalierte BBox
print(f"✅ Verwende gespeicherte BBox: {bbox_in_512}")
else:
#BBox-Koordinaten korrekt transformieren
#Die BBox-Koordinaten müssen vom Originalbild nach 512x512 transformiert werden
bbox_scaled = (
int(bbox_coords[0] * scale_factor),
int(bbox_coords[1] * scale_factor),
int(bbox_coords[2] * scale_factor),
int(bbox_coords[3] * scale_factor)
)
#Mit den Padding-Offsets wird bei nicht quadratischen 512x512 Bildern das Padding hinzugefügt
bbox_in_512 = (
bbox_scaled[0] + x_offset,
bbox_scaled[1] + y_offset,
bbox_scaled[2] + x_offset,
bbox_scaled[3] + y_offset
)
print(f"🔍 [COMPOSIT] Original-BBox: {bbox_coords}")
print(f"🔍 [COMPOSIT] Scale/Offset: {scale_factor}, ({x_offset},{y_offset})")
print(f"🔍 [COMPOSIT] BBox in 512: {bbox_in_512}")
print(f"🔍 [COMPOSIT] Inpaint Size: {inpaint_result.size}")
# Die BBox-Koordinaten sind durch 2 Punkte gegeben: oben links (x,y)-unten rechts (x,y)
# Prüfung: hat BBox gültige Koordinaten
if bbox_in_512[2] > bbox_in_512[0] and bbox_in_512[3] > bbox_in_512[1]:
# Bearbeiteten Bereich aus dem 512×512-Ergebnis ausschneiden in Größe der 512x512-skalierten BBox
edited_region = inpaint_result.crop(bbox_in_512)
print(f"🔍 [CROP] Ausgeschnitten: {edited_region.size}")
# Damit wird der 512er BBox-Inhalt auf Originalgröße-BBox hochskaliert
original_bbox_size = (bbox_coords[2] - bbox_coords[0],
bbox_coords[3] - bbox_coords[1])
edited_region_fullsize = edited_region.resize(
original_bbox_size,
Image.Resampling.LANCZOS
)
print(f"🔍 [RESIZE] Original-BBox-Size: {original_bbox_size}")
print(f"🔍 [RESIZE] Hochskaliert auf: {edited_region_fullsize.size}")
# SAM-Maske= original_mask in Originalgröße (also Smartphone: 4032x3024). Aus dieser Maske muß nun der
# Original BBox-Bereich ausgeschnitten werden und
mask_cropped = original_mask.crop(bbox_coords)
print(f"🔍 [MASK] Mask-Crop Size: {mask_cropped.size}")
# der Randbereich des BBox-Ausschnittes muß für Übergänge weich gezeichnet werden
soft_mask = mask_cropped.filter(ImageFilter.GaussianBlur(3))
# Alpha-Compositing mit präziser SAM-Maske
# damit wird auf den neu generirten BBox-Bereich in Originalgröße eine Folie gezogen
edited_rgba = edited_region_fullsize.convert("RGBA")
# Dadurch werden in die Folie der weichen SAM-Maske wieder an den Stellen schwarze/transparente Löcher
# gerissen wo der Hintergrund innerhalb der BBox bleiben muß!
mask_inverted = Image.eval(soft_mask, lambda x: 255 - x) #invertieren
mask_rgba = mask_inverted.convert("L") # SAM-Maske als Alpha-Kanal also als Löcherfolie
print(f"🔍 Alpha-Maske Werte: min={np.array(mask_rgba).min()}, max={np.array(mask_rgba).max()}")
print(f"🔍 Generierte Person Alpha: {edited_rgba.getchannel('A').getextrema()}")
# generiere hiermit ein neues transparantes Bild in original BBox-Größe (unsichtbare Trägerfolie)
temp_image = Image.new("RGBA", original_bbox_size, (0, 0, 0, 0))
# darauf klebe ich die neu generierte Person edited_rgba und SAM-Maske als Löcher-Folie-mask_rgba
temp_image.paste(edited_rgba, (0, 0), mask_rgba)
# hiermit hole ich mir den Hintergrund außerhalb der BBox zurück!
final_image.paste(temp_image, (bbox_coords[0], bbox_coords[1]), temp_image)
# Debug-Info
print(f"🔍 DEBUG COMPOSITING:")
print(f" Original BBox: {bbox_coords}")
print(f" Scale Factor: {scale_factor}")
print(f" Offsets: ({x_offset}, {y_offset})")
print(f" Inpaint Size: {inpaint_result.size}")
print(f"✅ Korrektes Compositing abgeschlossen. Finale Größe: {final_image.size}")
return final_image.convert("RGB")
def auto_detect_face_area(image):
"""Optimierten Vorschlag für Gesichtsbereich ohne externe Bibliotheken"""
width, height = image.size
face_size = min(width, height) * 0.4
x1 = (width - face_size) / 2
y1 = (height - face_size) / 4
x2 = x1 + face_size
y2 = y1 + face_size * 1.2
# Sortiere Koordinaten und stelle sicher, dass sie innerhalb des Bildes liegen
x1 = max(0, int(min(x1, x2)))
y1 = max(0, int(min(y1, y2)))
x2 = min(width, int(max(x1, x2)))
y2 = min(height, int(max(y1, y2)))
print(f"Geschätzte Gesichtskoordinaten: [{x1}, {y1}, {x2}, {y2}] (Bild: {width}x{height})")
return [x1, y1, x2, y2]
# === PIPELINES ===
pipe_txt2img = None
current_pipe_model_id = None
pipe_img2img = None
pipe_img2img_pose = None
pipe_img2img_depth = None
#Das Laden des Modells bedeutet, die trainierten Gewichte (Parameter) von der Festplatte zu lesen und
#im Arbeitsspeicher (RAM) und idealerweise im Grafikspeicher (VRAM) zu halten, damit sie für Berechnungen schnell verfügbar sind.
#die Funktion load_txt2img() verwaltet zwei separate Pipeline-Instanzen (für SD 1.5 und Realistic Vision) und gibt je nach
#Modellauswahl (model_id) die entsprechende Instanz zurück.
def load_txt2img(model_id):
"""Lädt das Text-to-Image Modell basierend auf der Auswahl"""
global pipe_txt2img, current_pipe_model_id
if pipe_txt2img is not None and current_pipe_model_id == model_id:
print(f"✅ Modell {model_id} bereits geladen")
return pipe_txt2img
print(f"🔄 Lade Modell: {model_id}")
config = MODEL_CONFIGS.get(model_id, MODEL_CONFIGS["runwayml/stable-diffusion-v1-5"])
print(f"📋 Modell-Konfiguration: {config['name']}")
print(f"📝 Beschreibung: {config['description']}")
try:
# VAE-Handling basierend auf Modellkonfiguration (Realistic Vision hat kein VAE-der Autoencoder ist ein CNN)
vae = None
if config.get("requires_vae", False):
print(f"🔧 Lade externe VAE: {config['vae_model']}")
try:
vae = AutoencoderKL.from_pretrained(
config["vae_model"],
torch_dtype=torch_dtype
).to(device)
print("✅ VAE erfolgreich geladen")
except Exception as vae_error:
print(f"⚠️ Fehler beim Laden der VAE: {vae_error}")
print("ℹ️ Versuche ohne VAE weiter...")
vae = None
model_params = {
"torch_dtype": torch_dtype,
"safety_checker": None,
"requires_safety_checker": False,
"add_watermarker": False,
"cache_dir": "/tmp/models" # Für Hugging Face Spaces
}
# Die Modelle haben unterscgiedliche Gewichtsformate. Safetensors neu und schneller Zugriff!
if model_id == "SG161222/Realistic_Vision_V6.0_B1_noVAE":
model_params["allow_pickle"] = False # WICHTIG für PyTorch 2.6
model_params["use_safetensors"] = False
print("⚠️ Realistic Vision Modell - Nutzt .bin-Dateien.")
else:
model_params["allow_pickle"] = True
model_params["use_safetensors"] = True
print("✅ Verwende SafeTensors für sicheres Laden.")
if config.get("supports_fp16", False) and torch_dtype == torch.float16:
model_params["variant"] = "fp16"
print("ℹ️ Verwende FP16 Variante")
else:
print("ℹ️ Verwende Standard Variante (kein FP16)")
if vae is not None:
model_params["vae"] = vae
print(f"📥 Lade Hauptmodell von Hugging Face...")
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
model_id,
**model_params
).to(device)
# Der Scheduler (z.B. DPM-Solver++ oder PNDM) ist der Algorithmus, der den Zeitplan für das schrittweise Entrauschen (Denoising)
# festlegt - er bestimmt, wie viele und welche Rauschschritte in welcher Reihenfolge abgearbeitet werden.
print("⚙️ Konfiguriere Scheduler...")
if pipe_txt2img.scheduler is None:
print("⚠️ Scheduler ist None, setze Standard-Scheduler")
pipe_txt2img.scheduler = PNDMScheduler.from_pretrained(
model_id,
subfolder="scheduler"
)
try:
if hasattr(pipe_txt2img.scheduler, 'config'):
scheduler_config = pipe_txt2img.scheduler.config
else:
scheduler_config = {
"beta_start": 0.00085,
"beta_end": 0.012,
"beta_schedule": "scaled_linear",
"num_train_timesteps": 1000,
"prediction_type": "epsilon",
"steps_offset": 1
}
print("⚠️ Keine Scheduler-Konfig gefunden, verwende Standard")
pipe_txt2img.scheduler = DPMSolverMultistepScheduler.from_config(
scheduler_config,
use_karras_sigmas=True,
algorithm_type="sde-dpmsolver++"
)
print("✅ DPM-Solver Multistep Scheduler konfiguriert")
except Exception as scheduler_error:
print(f"⚠️ Konnte DPM-Scheduler nicht setzen: {scheduler_error}")
print("ℹ️ Verwende Standard-Scheduler weiter")
pipe_txt2img.enable_attention_slicing()
print("✅ Attention Slicing aktiviert")
# Attention Slicing ist Aufteilung der Attention-Matrix auf die Heads -> späteres concat
if hasattr(pipe_txt2img, 'vae') and pipe_txt2img.vae is not None:
try:
pipe_txt2img.enable_vae_slicing()
if hasattr(pipe_txt2img.vae, 'enable_slicing'):
pipe_txt2img.vae.enable_slicing()
print("✅ VAE Slicing aktiviert")
except Exception as vae_slice_error:
print(f"⚠️ VAE Slicing nicht möglich: {vae_slice_error}")
current_pipe_model_id = model_id
print(f"✅ {config['name']} erfolgreich geladen")
print(f"📊 Modell-Dtype: {pipe_txt2img.dtype}")
print(f"📊 Scheduler: {type(pipe_txt2img.scheduler).__name__}")
print(f"⚙️ Empfohlene Einstellungen: Steps={config['recommended_steps']}, CFG={config['recommended_cfg']}")
return pipe_txt2img
except Exception as e:
print(f"❌ Fehler beim Laden von {model_id}: {str(e)[:200]}...")
import traceback
traceback.print_exc()
print("🔄 Fallback auf SD 1.5...")
try:
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch_dtype,
use_safetensors=True,
).to(device)
pipe_txt2img.enable_attention_slicing()
current_pipe_model_id = "runwayml/stable-diffusion-v1-5"
print("✅ Fallback auf SD 1.5 erfolgreich")
return pipe_txt2img
except Exception as fallback_error:
print(f"❌ Auch Fallback fehlgeschlagen: {fallback_error}")
raise
def load_img2img(keep_environment=False):
global pipe_img2img_pose, pipe_img2img_depth
# Initialisiere globale Variablen, falls noch nicht geschehen
if 'pipe_img2img_pose' not in globals():
pipe_img2img_pose = None
if 'pipe_img2img_depth' not in globals():
pipe_img2img_depth = None
if keep_environment:
# ===== MODUS: Depth + Canny =====
if pipe_img2img_depth is None:
print("🔄 Lade Multi-ControlNet-Inpainting-Modell (Depth + Canny)...")
try:
# LADE BEIDE ControlNet-Modelle für Depth-Modus
controlnet_depth = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-depth",
torch_dtype=torch_dtype
)
controlnet_canny = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny",
torch_dtype=torch_dtype
)
# WICHTIG: Reihenfolge muss mit prepare_controlnet_maps übereinstimmen!
# [Depth, Canny]
pipe_img2img_depth = StableDiffusionControlNetInpaintPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=[controlnet_depth, controlnet_canny], # Depth zuerst!
torch_dtype=torch_dtype,
safety_checker=None,
requires_safety_checker=False,
cache_dir="/tmp/models",
use_safetensors=True
).to(device)
# Scheduler konfigurieren
pipe_img2img_depth.scheduler = DPMSolverMultistepScheduler.from_config(
pipe_img2img_depth.scheduler.config,
algorithm_type="sde-dpmsolver++",
use_karras_sigmas=True,
timestep_spacing="trailing"
)
# Optimierungen
pipe_img2img_depth.enable_attention_slicing()
print("✅ Multi-ControlNet-Inpainting-Pipeline geladen (Depth + Canny)")
except Exception as e:
print(f"❌ Fehler beim Laden der Depth+Canny Pipeline: {e}")
raise
return pipe_img2img_depth
else:
# ===== MODUS: OpenPose + Canny =====
if pipe_img2img_pose is None:
print("🔄 Lade Multi-ControlNet-Inpainting-Modell (OpenPose + Canny)...")
try:
# LADE BEIDE ControlNet-Modelle für Pose-Modus
controlnet_openpose = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-openpose",
torch_dtype=torch_dtype
)
controlnet_canny = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny",
torch_dtype=torch_dtype
)
# WICHTIG: Reihenfolge muss mit prepare_controlnet_maps übereinstimmen!
# [OpenPose, Canny]
pipe_img2img_pose = StableDiffusionControlNetInpaintPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=[controlnet_openpose, controlnet_canny], # OpenPose zuerst!
torch_dtype=torch_dtype,
safety_checker=None,
requires_safety_checker=False,
cache_dir="/tmp/models",
use_safetensors=True
).to(device)
# Scheduler konfigurieren
pipe_img2img_pose.scheduler = DPMSolverMultistepScheduler.from_config(
pipe_img2img_pose.scheduler.config,
algorithm_type="sde-dpmsolver++",
use_karras_sigmas=True,
timestep_spacing="trailing"
)
# Optimierungen
pipe_img2img_pose.enable_attention_slicing()
print("✅ Multi-ControlNet-Inpainting-Pipeline geladen (OpenPose + Canny)")
except Exception as e:
print(f"❌ Fehler beim Laden der OpenPose+Canny Pipeline: {e}")
raise
return pipe_img2img_pose
#Die Callback-Funktion wird von der Pipeline nach jedem Verarbeitungsschritt aufgerufen und erhält Informationen
#wie den aktuellen step und timestep. Diese nutzt der Progressbalken-Callback, um den Fortschritt zu berechnen und anzuzeigen.
# === CALLBACK-FUNKTIONEN FÜR FORTSCHRITT ===
class TextToImageProgressCallback:
def __init__(self, progress, total_steps):
self.progress = progress
self.total_steps = total_steps
self.current_step = 0
def __call__(self, pipe, step, timestep, callback_kwargs):
self.current_step = step + 1
progress_percent = (step / self.total_steps) * 100
self.progress(progress_percent / 100, desc="Generierung läuft...")
return callback_kwargs
class ImageToImageProgressCallback:
def __init__(self, progress, total_steps, strength):
self.progress = progress
self.total_steps = total_steps
self.current_step = 0
self.strength = strength
self.actual_total_steps = None
def __call__(self, pipe, step, timestep, callback_kwargs):
self.current_step = step + 1
if self.actual_total_steps is None:
self.actual_total_steps = int(self.total_steps * self.strength)
print(f"🎯 Steps: {self.total_steps} × {self.strength} → {self.actual_total_steps} tatsächliche Denoising-Schritte")
progress_percent = (step / self.actual_total_steps) * 100
self.progress(progress_percent / 100, desc="Generierung läuft...")
return callback_kwargs
# === NEUE FUNKTIONEN FÜR DIE FEATURES (ANGEPASST FÜR 3 MODI) ===
def create_preview_image(image, bbox_coords, mode):
"""
NEUE FUNKTION: Erstellt Vorschau basierend auf 3 Modi mit farbigen Rahmen
Parameter:
- image: PIL Image
- bbox_coords: [x1, y1, x2, y2]
- mode: "environment_change", "focus_change", "face_only_change"
Returns:
- PIL Image mit farbigem Rahmen und Text
"""
if image is None:
return None
preview = image.copy()
draw = ImageDraw.Draw(preview)
# Farben basierend auf Modus
if mode == "environment_change":
border_color = (0, 255, 0, 180) # Grün für Umgebung
mode_text = "UMGEBUNG ÄNDERN (Bereich geschützt)"
box_color = (255, 255, 0, 200) # Gelb für geschützten Bereich
text_bg_color = (0, 128, 0, 160) # Dunkelgrün
elif mode == "focus_change":
border_color = (255, 165, 0, 180) # Orange für Focus
mode_text = "FOCUS VERÄNDERN (Bereich+Körper)"
box_color = (255, 0, 0, 200) # Rot für Veränderungsbereich
text_bg_color = (255, 140, 0, 160) # Dunkelorange
elif mode == "face_only_change":
border_color = (255, 0, 0, 180) # Rot für nur Gesicht
mode_text = "NUR BEREICH VERÄNDERN"
box_color = (255, 0, 0, 200) # Rot für Veränderungsbereich
text_bg_color = (128, 0, 0, 160) # Dunkelrot
else:
# Fallback
border_color = (128, 128, 128, 180)
mode_text = "UNBEKANNTER MODUS"
box_color = (128, 128, 128, 200)
text_bg_color = (64, 64, 64, 160)
# Skaliere Rahmendicke basierend auf Bildgröße (sonst bei großen Bildern ganz dünne Rahmen!)
border_width = max(8, image.width // 200) # Mindestens 8px, bei großen Bildern dicker
draw.rectangle([0, 0, preview.width-1, preview.height-1],
outline=border_color, width=border_width)
if bbox_coords and all(coord is not None for coord in bbox_coords):
# Sortiere Koordinaten
x1, y1, x2, y2 = sort_coordinates(*bbox_coords)
# Stelle sicher, dass die Koordinaten innerhalb des Bildes liegen
x1 = max(0, min(x1, preview.width-1))
y1 = max(0, min(y1, preview.height-1))
x2 = max(0, min(x2, preview.width-1))
y2 = max(0, min(y2, preview.height-1))
# Nur zeichnen, wenn die Bounding Box gültig ist
if x2 > x1 and y2 > y1:
# Skaliere Box-Rahmen basierend auf Bildgröße
box_width = max(3, image.width // 400)
draw.rectangle([x1, y1, x2, y2], outline=box_color, width=box_width)
text_color = (255, 255, 255)
# Text über der Bounding Box platzieren
text_y = max(0, y1 - 25)
text_bbox = draw.textbbox((x1, text_y), mode_text)
draw.rectangle([text_bbox[0]-5, text_bbox[1]-2, text_bbox[2]+5, text_bbox[3]+2],
fill=text_bg_color)
draw.text((x1, text_y), mode_text, fill=text_color)
return preview
def update_live_preview(image, bbox_x1, bbox_y1, bbox_x2, bbox_y2, mode):
"""
Aktualisiert die Live-Vorschau bei Koordinaten-Änderungen
NEU: Verwendet 3 Modi statt Boolean
"""
if image is None:
return None
# Sortiere die Koordinaten (Slider zeigen Originalkoordinaten)
bbox_coords = sort_coordinates(bbox_x1, bbox_y1, bbox_x2, bbox_y2)
return create_preview_image(image, bbox_coords, mode)
def process_image_upload(image):
"""Verarbeitet Bild-Upload -wenn kein Bild hochgeladen wird None zurückgegeben-> kein Absturz! und gibt Bild + Koordinaten zurück"""
if image is None:
return None, None, None, None, None
width, height = image.size
# Berechne Bounding-Box basierend auf der tatsächlichen Bildgröße
bbox = auto_detect_face_area(image)
# Sortiere die Koordinaten
bbox_x1, bbox_y1, bbox_x2, bbox_y2 = sort_coordinates(*bbox)
# Für die Vorschau verwende die Originalkoordinaten
preview = create_preview_image(image, [bbox_x1, bbox_y1, bbox_x2, bbox_y2], "environment_change")
# Slider-Werte SIND JETZT ORIGINALKOORDINATEN (keine 512-Skalierung!)
print(f"Bild {width}x{height} -> Slider-Originalwerte: [{bbox_x1}, {bbox_y1}, {bbox_x2}, {bbox_y2}]")
return preview, bbox_x1, bbox_y1, bbox_x2, bbox_y2
# === FUNKTION FÜR SLIDER-UPDATE ===
def update_slider_for_image(image):
"""Aktualisiert Slider-Maxima basierend auf Bildgröße bis 4096x4096"""
if image is None:
return (
gr.update(maximum=MAX_IMAGE_SIZE),
gr.update(maximum=MAX_IMAGE_SIZE),
gr.update(maximum=MAX_IMAGE_SIZE),
gr.update(maximum=MAX_IMAGE_SIZE)
)
width, height = image.size
# Setze Slider-Maxima auf Bildgröße (begrenzt auf MAX_IMAGE_SIZE für Stabilität)
max_width = min(width, MAX_IMAGE_SIZE)
max_height = min(height, MAX_IMAGE_SIZE)
print(f"Slider-Maxima gesetzt auf: {max_width}x{max_height}")
return (
gr.update(maximum=max_width),
gr.update(maximum=max_height),
gr.update(maximum=max_width),
gr.update(maximum=max_height)
)
def text_to_image(prompt, model_id, steps, guidance_scale, progress=gr.Progress()):
try:
if not prompt or not prompt.strip():
return None, "Bitte einen Prompt eingeben"
print("\n" + "="*80)
print(f"🚀 Starte Generierung mit Modell: {model_id}")
print("\n" + "="*80)
print(f"📝 Prompt: {prompt}")
# Automatische negative Prompts generieren
auto_negatives = auto_negative_prompt(prompt)
print(f"🤖 Automatisch generierte Negative Prompts: {auto_negatives}")
start_time = time.time()
# Liste von Qualitätswörtern/Gewichten, die auf Benutzereingaben prüfen
quality_keywords = ['masterpiece', 'best quality', 'high quality', 'highly detailed',
'exquisite', 'ultra detailed', 'professional',
'perfect', 'excellent', 'amazing', 'stunning', 'beautiful']
# Prüfe, ob der Benutzer bereits Qualitätswörter/Gewichte verwendet hat
user_has_quality_words = False
# Konvertiere Prompt zu Kleinbuchstaben für die Prüfung
prompt_lower = prompt.lower()
# Prüfe auf einfache Qualitätswörter
for keyword in quality_keywords:
if keyword in prompt_lower:
user_has_quality_words = True
print(f"✓ Benutzer verwendet bereits Qualitätswort: {keyword}")
break
# Prüfe auf Gewichte (z.B. (word:1.5), [word], etc.)
weight_patterns = [r'\([^)]+:\d+(\.\d+)?\)', r'\[[^\]]+\]']
for pattern in weight_patterns:
if re.search(pattern, prompt):
user_has_quality_words = True
print("✓ Benutzer verwendet bereits Gewichte im Prompt")
break
# Prompt basierend auf Prüfung anpassen
if not user_has_quality_words:
enhanced_prompt = f"masterpiece, best quality, {prompt}"
print(f"🔄 Verbesserter Prompt: {enhanced_prompt}")
else:
enhanced_prompt = prompt
print("✓ Benutzerprompt wird unverändert verwendet")
print(f"Finaler Prompt für Generation: {enhanced_prompt}")
progress(0, desc="Lade Modell...")
pipe = load_txt2img(model_id)
seed = random.randint(0, 2**32 - 1)
generator = torch.Generator(device=device).manual_seed(seed)
print(f"🌱 Seed: {seed}")
callback = TextToImageProgressCallback(progress, steps)
print(f"⚙️ Einstellungen: Steps={steps}, CFG={guidance_scale}")
image = pipe(
prompt=enhanced_prompt,
negative_prompt=auto_negatives,
height=512,
width=512,
num_inference_steps=int(steps),
guidance_scale=guidance_scale,
generator=generator,
callback_on_step_end=callback,
callback_on_step_end_tensor_inputs=[],
).images[0]
end_time = time.time()
duration = end_time - start_time
print(f"✅ Bild generiert in {duration:.2f} Sekunden")
config = MODEL_CONFIGS.get(model_id, MODEL_CONFIGS["runwayml/stable-diffusion-v1-5"])
status_msg = f"✅ Generiert mit {config['name']} in {duration:.1f}s"
return image, status_msg
except Exception as e:
error_msg = f"❌ Fehler: {str(e)}"
print(f"❌ Fehler in text_to_image: {e}")
import traceback
traceback.print_exc()
return None, error_msg
def img_to_image(image, prompt, neg_prompt, strength, steps, guidance_scale,
mode, bbox_x1, bbox_y1, bbox_x2, bbox_y2,
progress=gr.Progress()):
"""
KORRIGIERTE HAUPTFUNKTION FÜR CONTROLNET-GESTEUERTES INPAINTING
"""
try:
if image is None:
return None, None, None, None, None
import time, random
start_time = time.time()
print("\n" + "="*80)
print(f"🚀 Img2Img Start → Modus: {mode}")
print("\n" + "="*80)
print(f"📊 Einstellungen: Strength: {strength}, Steps: {steps}, Guidance: {guidance_scale}")
print(f"📝 Prompt: {prompt}")
print(f"🚫 Negativ-Prompt: {neg_prompt}")
final_image = None # Variable wird initiiert!
# ===== AUTOMATISCHEN NEGATIV-PROMPT GENERIEREN =====
auto_negatives = auto_negative_prompt(prompt)
print(f"🤖 Automatisch generierter Negativ-Prompt: {auto_negatives}")
# ===== KOMBINIERE MANUELLEN UND AUTOMATISCHEN PROMPT =====
combined_negative_prompt = ""
if neg_prompt and neg_prompt.strip():
user_neg = neg_prompt.strip()
print(f"👤 Benutzer Negativ-Prompt: {user_neg}")
user_words = [word.strip().lower() for word in user_neg.split(",")]
auto_words = [word.strip().lower() for word in auto_negatives.split(",")]
combined_words = user_words.copy()
for auto_word in auto_words:
if auto_word and auto_word not in user_words:
combined_words.append(auto_word)
unique_words = []
seen_words = set()
for word in combined_words:
if word and word not in seen_words:
unique_words.append(word)
seen_words.add(word)
combined_negative_prompt = ", ".join(unique_words)
else:
combined_negative_prompt = auto_negatives
print(f"ℹ️ Kein manueller Negativ-Prompt, verwende nur automatischen: {combined_negative_prompt}")
print(f"✅ Finaler kombinierter Negativ-Prompt: {combined_negative_prompt}")
# ===== PROMPT-BOOSTER FÜR DREI MODI =====
if mode == "face_only_change":
prompt_lower = prompt.lower()
front_face_keywords = [
"portrait", "face", "eyes", "smile", "lips", "nose", "expression",
"looking at camera", "frontal view", "headshot", "selfie", "close-up",
"profile", "side view", "front", "frontal", "facing camera", "jawline"
]
back_head_keywords = [
"back of head", "from behind", "rear view", "looking away",
"turned away", "back view", "backside", "back", "rear",
"hair only", "ponytail", "hairstyle", "hair", "back hair"
]
# Bestimme ob Gesicht vorne oder Hinterkopf vorne
is_front_face = any(keyword in prompt_lower for keyword in front_face_keywords)
is_back_head = any(keyword in prompt_lower for keyword in back_head_keywords)
# Fallback: Wenn keine spezifischen Keywords, annehmen es ist Gesicht
if not is_front_face and not is_back_head:
is_front_face = True # Standard: Gesicht vorne
print(" ℹ️ Keine Gesicht/Hinterkopf-Keywords → Standard: Gesicht vorne")
print(f" 🎯 Gesichtserkenner für Boosters: Vorne={is_front_face}, Hinten={is_back_head}")
# NUR für frontale Gesichter Gesichts-Booster hinzufügen
if is_front_face and not is_back_head:
face_boosters = "(perfect face:1.2), (symmetrical face:1.1), realistic shaded perfect face, "
if not any(keyword in prompt_lower for keyword in
["perfect face", "symmetrical", "realistic face", "shaded face"]):
enhanced_prompt = face_boosters + prompt
print(f"👤 Gesichts-Booster hinzugefügt: {face_boosters}")
else:
enhanced_prompt = prompt
print(f"👤 Benutzer hat bereits Gesichts-Booster im Prompt")
else:
# Keine Gesichts-Booster für Hinterkopf oder unklare Fälle
enhanced_prompt = prompt
if is_back_head:
print(f"💇 Hinterkopf erkannt → Keine Gesichts-Booster")
else:
print(f"👤 Keine Gesichts-Booster (unspezifischer Prompt)")
#face_boosters = "(perfect face:1.2), (symmetrical face:1.1), realistic shaded perfect face, "
#if not any(keyword in prompt.lower() for keyword in
# ["perfect face", "symmetrical", "realistic face", "shaded face"]):
# enhanced_prompt = face_boosters + prompt
# print(f"👤 Gesichts-Booster hinzugefügt: {face_boosters}")
#else:
# enhanced_prompt = prompt
# print(f"👤 Benutzer hat bereits Gesichts-Booster im Prompt")
elif mode == "focus_change":
focus_boosters = "(sharp focus:1.2), (detailed subject:1.1), (clear foreground:1.1), "
if not any(keyword in prompt.lower() for keyword in
["sharp focus", "detailed subject", "clear foreground", "well-defined"]):
enhanced_prompt = focus_boosters + prompt
print(f"🎯 Focus-Booster hinzugefügt: {focus_boosters}")
else:
enhanced_prompt = prompt
print(f"🎯 Benutzer hat bereits Focus-Booster im Prompt")
elif mode == "environment_change":
background_boosters = "complete scene, full background, entire environment, "
if not any(keyword in prompt.lower() for keyword in
["complete scene", "full background", "entire environment", "whole setting"]):
enhanced_prompt = background_boosters + prompt
print(f"🌳 Hintergrund-Booster hinzugefügt: {background_boosters}")
else:
enhanced_prompt = prompt
print(f"🌳 Benutzer hat bereits Hintergrund-Booster im Prompt")
else:
enhanced_prompt = prompt
print(f"🎯 Finaler Prompt für {mode}: {enhanced_prompt}")
progress(0, desc="Starte Generierung...")
####################################################################################################################################
# ===== OPTIMIERTE MODUS-SPEZIFISCHE EINSTELLUNGEN FÜR CONTROLNET =====
# Je radikaler die Veränderung, desto weniger ControlNet braucht man!
# Radikale Veränderung (Struktur komplett anders-Mensch/Auto):controlnet_strength = 0.3
# STRUKTURELLER WECHSEL (Anatomie ändert sich-Mensch/Tier): controlnet_strength = 0.4-0.5
# Detailveränderung (gleiche Struktur-Mensch/Hexe):controlnet_strength = 0.5-0.6
# Je spezifischer ich weiß, WAS genau gleich bleiben soll, desto gezielter wähle ich die entsprechende ControlNet-Map.
# Demnach ist ControlNet für den Erhalt der gewünschten Bildobjekte. Canny-erhält Kanten, Pose-Köperhalten, Depth-Tiefeninformationen
####################################################################################################################################
if mode == "focus_change":
# Optimale UI-Werte (für alle focus_change-Fälle):
# Strength: 0.55 – 0.6 → ideal: 0.58
# Guidance (CFG): 7.5 – 8 → ideal: 8
# Steps: 32 – 36
keep_environment = False
# adj_strength ist die Denoising-Stärke.0.1-0.3: Leichte Veränderung (behält Original)
# 0.4-0.6: Mittlere Veränderung, 0.7-0.9: Starke Veränderung
adj_strength = min(0.6, strength)
# CONTROLNET-STÄRKE=Anteil der Controlnet-Kontrolle die an Inpaint weitergegeben werden soll
# 0.3-0.5: Wenig Kontrolle → Inpaint hat mehr Freiheit, 0.6-0.8: Mittlere Kontrolle → Balance
# 0.9: Starke Kontrolle → Inpaint folgt streng ControlNet
controlnet_strength = 0.5 # Stärkere ControlNet-Kontrolle für Inpaint
# Standard-Ratio (Controlnet gesteuertes Inpainting) - Ratio entscheidet über Anatomie und Stilfreiheit
pose_ratio = 0.7 # 70%
canny_ratio = 0.3 # 30%
# Konvertiert den gesamten Prompt in Kleinbuchstaben um ggf. bei den keywords zu mappen
prompt_lower = prompt.lower()
# Keyword-Gruppen
humanoid_keywords = [
"anime", "cartoon", "manga", "witch", "wizard", "sorcerer",
"alien", "elf", "fairy", "character", "fantasy", "superhero",
"cyborg", "robot", "android", "santa", "person", "woman", "man",
"girl", "boy", "child", "business", "suit", "professional",
"sports", "athlete", "runner", "dancer", "portrait", "face"
]
object_keywords = [
"car", "vehicle", "automobile", "chair", "table", "desk",
"statue", "sculpture", "monument", "lamp", "bottle", "vase",
"product", "object", "furniture", "device", "tool", "item",
"building", "house", "tree", "plant", "rock", "stone"
]
animal_keywords = [
"dog", "cat", "wolf", "lion", "tiger", "bear", "rabbit",
"horse", "bird", "animal", "creature", "beast", "monkey",
"elephant", "giraffe", "zebra", "deer", "fox", "pet"
]
# Anpassung für Humanoid → Humanoid
#if any(keyword in prompt_lower for keyword in humanoid_keywords):
# adj_strength = 0.5 # wie stark entrauscht wird. Wenn Bereiche transparent oft nicht genug entrauscht. Strukturveränderung ist nicht sehr hoch-niedriger Wert!
# controlnet_strength = 0.8 # controlnet_strength runter: Reduziert global und gleichmäßig den Einfluss beider Maps. Man kann auch beide Maps einzeln heruntersetzen. Ist das Gleiche!
# pose_ratio = 0.60 # 95%Pose, 5%Canny - wenn Pose gehalten und kaum Detailveränderung-
# canny_ratio = 0.10 # wenn Pose gehalten und mehr Detailveränderung -empfohlen 0.85/0.15, canny schlecht für Anime
# Achtung: Hoher strength-Wert im UI (0.85) instabiler. Zwar radikale Änderung aber ggf verdrehte Körper, fehlende Gliedmaße, extra Glieder, verzerrte Proportionen
if any(keyword in prompt_lower for keyword in humanoid_keywords):
# Der Parameter 'strength' ist die UI-Veränderungsstärke d.h. die Einstellungen
# für Controlnet werden anhand von strength berechnet. Nun muß über Prompt Finetuning erfolgen!
ui_strength = strength
# Smoothstep-Hilfsfunktion sorgt für weiche Übergänge zwischen den Posen. Die Smoothstep-Funktion sorgt dafür
# dass am Anfang und Ende des UI-Strength-Wertes nicht viel passiert hauptsächlich in der Mitte
def smoothstep(min_val, max_val, x):
x = max(0, min(1, (x - min_val) / (max_val - min_val)))
return x * x * (3 - 2 * x)
# 1. Basierend auf der UI-Stärke (strength) berechnen
adj_strength = 0.15 + 0.8 * ui_strength
if ui_strength <= 0.7:
# AB UI=0.7: Höhere ControlNet-Stärke für Stabilität
controlnet_strength = 0.85 - 0.83 * ui_strength # 0.85 → 0.265 bei UI=0.9
else:
# BIS UI=0.7: controlnet wird für mehr Freiheit reduziert
t = (ui_strength - 0.7) / 0.2
controlnet_strength = 0.269 + 0.081 * t # 0.85 → 0.27 bei UI=0.7
# 2. DYNAMISCHE POSE-ANPASSUNG: Mehr Pose bei hoher Stärke für Stabilität!
# Standard: 0.85 → 0.45 (wie bisher)
base_pose = 0.85 - 0.4 * smoothstep(0.4, 0.8, ui_strength)
# Canny-Reduktion bei hoher Stärke für Farbfreiheit
if ui_strength > 0.6:
# Je höher die Stärke, desto weniger Canny (für Farbänderungen)
canny_reduction = smoothstep(0.6, 0.9, ui_strength) * 0.15
pose_ratio = min(0.60, base_pose + canny_reduction) # Pose erhöhen = Canny reduzieren
else:
pose_ratio = base_pose
canny_ratio = 1.0 - pose_ratio
# 2. Werte auf sinnvolle Bereiche begrenzen (Clipping)
adj_strength = max(0.15, min(adj_strength, 0.95))
controlnet_strength = max(0.12, min(controlnet_strength, 0.85))
pose_ratio = max(0.45, min(pose_ratio, 0.60))
canny_ratio = max(0.40, min(canny_ratio, 0.55))
conditioning_scale = [
controlnet_strength * pose_ratio, # Depth-Gewichtung
controlnet_strength * canny_ratio # Canny-Gewichtung
]
print(f"👤 Humanoid → Humanoid (UI-Stärke: {ui_strength})")
print(f" adj_strength: {adj_strength:.2f}, controlnet: {controlnet_strength:.2f}")
print(f" Verhältnis: Pose {pose_ratio*100:.0f}% : Canny {canny_ratio*100:.0f}%")
print(f" Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
# Debug:
print(f"DEBUG UI={ui_strength}: smoothstep={smoothstep(0.4, 0.8, ui_strength):.3f}")
print(f"DEBUG Pose vor Clipping: {0.85 - 0.4 * smoothstep(0.4, 0.8, ui_strength):.3f}")
print(f"DEBUG Pose nach Clipping: {pose_ratio:.3f}")
# Anpassung für Gegenstand → Gegenstand
elif any(keyword in prompt_lower for keyword in object_keywords):
adj_strength = min(0.7, strength * 1.15)
controlnet_strength = 0.5
pose_ratio = 0.10 # 10% Pose
canny_ratio = 0.90 # 90% Canny
conditioning_scale = [
controlnet_strength * pose_ratio, # Depth-Gewichtung
controlnet_strength * canny_ratio # Canny-Gewichtung
]
print("📦 Gegenstand → Gegenstand → Ratio 25:75 (Pose:Canny)")
# Anpassung für Mensch → Tier
elif any(keyword in prompt_lower for keyword in animal_keywords):
adj_strength = min(0.6, strength * 1.1)
controlnet_strength = 0.5
pose_ratio = 0.5 # 50% Pose - empfohlen 0.45/0.55
canny_ratio = 0.5 # 50% Canny
print("🐾 Mensch → Tier → Ratio 50:50 (Pose:Canny)")
# CONDITIONING SCALE BERECHNEN (genau wie environment_change)
conditioning_scale = [
controlnet_strength * pose_ratio, # OpenPose
controlnet_strength * canny_ratio # Canny
]
else: #Standard
# CLIPPING
adj_strength = max(0.4, min(adj_strength, 0.8))
controlnet_strength = max(0.3, min(controlnet_strength, 0.7))
pose_ratio = max(0.5, min(pose_ratio, 0.8))
canny_ratio = max(0.2, min(canny_ratio, 0.5))
# CONDITIONING_SCALE FEHLT HIER!
conditioning_scale = [
controlnet_strength * pose_ratio, # OpenPose
controlnet_strength * canny_ratio # Canny
]
print(f"🎯 MODUS: Focus verändern")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" OpenPose: {pose_ratio*100}%, Canny: {canny_ratio*100}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
elif mode == "environment_change":
# optimale UI-Werte:
# Strength: 0.72 – 0.78 → ideal: 0.75
# guidance (CFG): 8.5 – 9.5 → ideal: 9
# Steps: 34 – 38 → ideal: 35
keep_environment = True
ui_strength = strength # Veränderungsstärke 0.1-0.9
#wandelt den Prompt in Kleinbuchstaben um (Keywords!)
prompt_lower = prompt.lower()
#Standardfall:wird genutzt wenn Prompt nicht eines der keywords unten beinhaltet
# Denoising: starke Neugenerierung
adj_strength = 0.75 # Leicht runter auf Realismus
# CONTROLNET-STÄRKE
controlnet_strength = 0.55 #Inpaint kann bei Neugenerierung nicht so viel Kontrolle vertragen
#Ratios: Controlnet gesteuertes Inpainting
depth_ratio = 0.50 # 35%
canny_ratio = 0.12 # 10%
# Heuristik für Naturszenen vs. Innenräume
nature_keywords = ["beach", "forest", "mountain", "ocean", "sky", "field", "landscape", "nature", "outdoor", "desert", "snow", "arctic"]
interior_keywords = ["office", "room", "interior", "kitchen", "bedroom", "living room", "indoor", "wall", "furniture"]
# Anpassung für Innenräume (mehr Kantenerhalt)
if any(keyword in prompt_lower for keyword in interior_keywords):
# Ob Formel korrekt? kein Test!
adj_strength = 0.2 + (ui_strength * 0.5)
controlnet_strength = 0.7 + (ui_strength * 0.2)
canny_ratio = 0.8 + (ui_strength * 0.1)
depth_ratio = 1.0 - canny_ratio
# Clipping: extreme Werte, Instabilität, Für exakten Objektschutz (Büro→Küche, Zimmer→Garten)
adj_strength = max(0.15, min(adj_strength, 0.7)) # Max 70% Denoising
controlnet_strength = max(0.6, min(controlnet_strength, 0.95)) # Min 60% ControlNet
canny_ratio = max(0.7, min(canny_ratio, 0.95)) # Canny mind. 70% für Kanten
depth_ratio = max(0.05, min(depth_ratio, 0.3)) # Depth max 30%
conditioning_scale = [
controlnet_strength * depth_ratio,
controlnet_strength * canny_ratio
]
print(f"🏠 INNENRÄUME: UI={ui_strength:.2f}")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" Depth: {depth_ratio*100:.0f}% (Maßstab), Canny: {canny_ratio*100:.0f}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
# Anpassung für Naturszenen (maximale Flexibilität) - die optimalen UI-Werte: strength:0,72 , steps: 35 , guidance: 9-9,5
elif any(keyword in prompt_lower for keyword in nature_keywords):
# DENOISING: Radikaler bei Naturszenen (Wald→Wüste)
adj_strength = 0.15 + (ui_strength * 0.75) # (0.15-0.9 linear) wie stark das bestehende Bild überschrieben wird im kompletten Denoising-Prozess-also Strukturveränderung
# CONTROLNET: WENIGER bei Naturszenen (mehr Freiheit)
controlnet_strength = 0.5 + (ui_strength * 0.25) # 0.3-0.6
# RATIOS: WENIG Canny (Kanten stören bei Naturveränderung)
# MEHR Depth (Maßstab/Tiefe erhalten)
depth_ratio = 0.9 - (ui_strength * 0.3) # Depth-Wert hält Maßstab und Boden (Emi groß, Liege klein - Emi im Meer)
canny_ratio = 1.0 - depth_ratio # erzwingt Kanten - hält dadurch an alter Umgebung fest - schlecht bei Umgebungswechsel
# Clipping verhindert extreme Werte, Controlnet hat immer etwas Einfluß sonst ist Pipline instabil
adj_strength = max(0.15, min(adj_strength, 0.95))
controlnet_strength = max(0.2, min(controlnet_strength, 0.6))
depth_ratio = max(0.5, min(depth_ratio, 0.95)) # Depth nicht unter 30%
canny_ratio = max(0.05, min(canny_ratio, 0.5)) # Canny nicht über 70%
conditioning_scale = [
controlnet_strength * depth_ratio,
controlnet_strength * canny_ratio
]
print(f"🌳 NATURSZENE: UI={ui_strength:.2f}")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" Depth: {depth_ratio*100:.0f}% (Maßstab), Canny: {canny_ratio*100:.0f}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
else: #Standard
# Clipping:
adj_strength = max(0.15, min(adj_strength, 0.7)) # Max 70% Denoising
controlnet_strength = max(0.6, min(controlnet_strength, 0.95)) # Min 60% ControlNet
canny_ratio = max(0.7, min(canny_ratio, 0.95)) # Canny mind. 70% für Kanten
depth_ratio = max(0.05, min(depth_ratio, 0.3)) # Depth max 30%
conditioning_scale = [
controlnet_strength * depth_ratio, # Depth-Gewichtung
controlnet_strength * canny_ratio # Canny-Gewichtung
]
print(f"🎯 STANDARD MODUS: Umgebung ändern")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" Depth: {depth_ratio*100}%, Canny: {canny_ratio*100}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
else: # face_only_change
keep_environment = True
ui_strength = strength # 0.1-0.9 vom User
prompt_lower = prompt.lower()
#Standard für alle Gesichter
adj_strength = 0.15 + (ui_strength * 0.75) # 0.15-0.9 Bereich
# ControlNet-Stärke (nimmt mit UI-Strength ab)
controlnet_strength = 0.8 - (ui_strength * 0.6) # 0.8 → 0.2 linear
# Depth vs. Canny Basis-Ratio
depth_ratio = 0.8 - (ui_strength * 0.4) # 0.8 → 0.4
canny_ratio = 0.2 + (ui_strength * 0.3) # 0.2 → 0.5
# Realistic/Photo-Stile
#realistic_keywords = ["photorealistic", "photography", "photo", "realistic", "portrait", "studio", "cinematic"]
# Zeichnungen/Illustrationen (kein Anime)
drawing_keywords = ["drawing", "illustration", "sketch", "painting", "artwork", "watercolor"]
# Anime-Stile
anime_keywords = ["anime", "manga", "cartoon", "character", "chibi", "cel-shading", "lineart"]
#front_face_keywords = [
# "portrait", "face", "eyes", "smile", "lips", "nose", "expression",
# "looking at camera", "frontal view", "headshot", "selfie", "close-up",
# "profile", "side view", "front", "frontal", "facing camera", "jawline"
#]
#back_head_keywords = [
# "back of head", "from behind", "rear view", "looking away",
# "turned away", "back view", "backside", "back", "rear",
# "hair only", "ponytail", "hairstyle", "hair", "back hair"
#]
# Bestimme ob Gesicht vorne oder Hinterkopf vorne
#is_front_face = any(keyword in prompt_lower for keyword in front_face_keywords)
#is_back_head = any(keyword in prompt_lower for keyword in back_head_keywords)
# Fallback: Wenn keine spezifischen Keywords, annehmen es ist Gesicht
#if not is_front_face and not is_back_head:
# is_front_face = True # Standard: Gesicht vorne
# print(" ℹ️ Keine Gesicht/Hinterkopf-Keywords → Standard: Gesicht vorne")
print(f" 🎯 Gesichtserkennung: Vorne={is_front_face}, Hinten={is_back_head}")
if any(keyword in prompt_lower for keyword in anime_keywords):
print("🎨 ANIME-TRANSFORM-MODUS")
def smoothstep(min_val, max_val, x):
x = max(0, min(1, (x - min_val) / (max_val - min_val)))
return x * x * (3 - 2 * x)
# Basiseinstellungen für Anime
adj_strength = 0.30 + 0.55 * smoothstep(0.35, 0.9, ui_strength)
adj_strength = max(0.3, min(adj_strength, 0.85))
controlnet_strength = 0.30 + 0.52 * smoothstep(0.65, 0.9, ui_strength)
controlnet_strength = max(0.25, min(controlnet_strength, 0.85))
# ANPASSUNG BASIEREND AUF GESICHT/HINTERKOPF
if is_front_face:
# Anime-GESICHT vorne
depth_ratio = 0.65 + 0.15 * smoothstep(0.5, 0.9, ui_strength) # Höher für Gesichtsstruktur
canny_ratio = 1.0 - depth_ratio
print(" 👤 Anime-Gesicht (vorne): Mehr Depth für 3D-Struktur")
elif is_back_head:
# Anime-HINTERKOPF
depth_ratio = 0.65 + 0.20 * smoothstep(0.5, 0.9, ui_strength) # Niedriger
canny_ratio = 1.0 - depth_ratio
# 2. CONTROLNET-BOOST AB 0.7 (neue Logik)
if ui_strength <= 0.7:
# Bis 0.7: normale Steigerung (wie getestet und gut)
controlnet_strength = 0.30 + 0.52 * smoothstep(0.65, 0.9, ui_strength)
else:
# Ab 0.7: DEUTLICH MEHR ControlNet für Stabilität
# Von 0.7 (≈0.5) auf 0.9 (≈0.85) linear steigern
boost_factor = (ui_strength - 0.7) / 0.2 # 0.0 → 1.0
controlnet_strength = 0.5 + (0.35 * boost_factor) # 0.5 → 0.85
# 3. CLIPPING (sicherheitshalber)
controlnet_strength = max(0.3, min(controlnet_strength, 0.9))
print(f" 💇 Anime-Hinterkopf: Depth={depth_ratio:.2f}, ControlNet={controlnet_strength:.2f}")
if ui_strength > 0.7:
print(" ⚡ BOOST: ControlNet erhöht für bessere Strukturerhaltung")
else:
# Standard-Anime (Fallback)
depth_ratio = 0.55 + 0.15 * smoothstep(0.5, 0.9, ui_strength)
canny_ratio = 1.0 - depth_ratio
conditioning_scale = [
controlnet_strength * depth_ratio,
controlnet_strength * canny_ratio
]
print(f"UI Strength: {ui_strength}")
print(f"adj_strength: {adj_strength:.3f}")
print(f"controlnet_strength: {controlnet_strength:.3f}")
print(f"Depth: {depth_ratio*100:.1f}%, Canny: {canny_ratio*100:.1f}%")
print(f"conditioning_scale: {conditioning_scale}")
elif any(keyword in prompt_lower for keyword in drawing_keywords):
# Weniger Denoising für anatomische Korrektheit (nicht getestet)
adj_strength = max(0.3, adj_strength * 0.9) # Konservativer
# Mehr ControlNet für Strukturerhalt
controlnet_strength = min(0.9, controlnet_strength * 1.2) # Mehr Kontrolle
# Mehr Depth, weniger Canny
depth_ratio = min(0.9, depth_ratio * 1.2)
canny_ratio = max(0.1, canny_ratio * 0.8) # Weniger Canny
#Clipping
adj_strength = max(0.15, min(adj_strength, 0.95))
controlnet_strength = max(0.1, min(controlnet_strength, 0.9))
depth_ratio = max(0.1, min(depth_ratio, 0.9))
canny_ratio = max(0.1, min(canny_ratio, 0.9))
conditioning_scale = [
controlnet_strength * depth_ratio,
controlnet_strength * canny_ratio
]
print(" 📸 Drawing-Modus: Mehr Strukturerhalt")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" Depth: {depth_ratio*100}%, Canny: {canny_ratio*100}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
else: #Standard
#Clipping
adj_strength = max(0.15, min(adj_strength, 0.95))
controlnet_strength = max(0.1, min(controlnet_strength, 0.9))
depth_ratio = max(0.1, min(depth_ratio, 0.9))
canny_ratio = max(0.1, min(canny_ratio, 0.9))
conditioning_scale = [
controlnet_strength * depth_ratio,
controlnet_strength * canny_ratio
]
print(" 📸 Standard-Modus: Mehr Strukturerhalt")
print(f" Strength: {adj_strength}, ControlNet: {controlnet_strength}")
print(f" Depth: {depth_ratio*100}%, Canny: {canny_ratio*100}%")
print(f" Conditioning Scale: [{conditioning_scale[0]:.3f}, {conditioning_scale[1]:.3f}]")
#################################################################################################
# Controlnet-Einstellungen ENDE
#################################################################################################
# ===== WICHTIG: VARIABLEN FÜR KOMPLETTEN WORKFLOW =====
original_mask = None
padding_info = None
scaled_image = None
scaled_mask = None
if bbox_x1 is not None and bbox_y1 is not None and bbox_x2 is not None and bbox_y2 is not None:
print(f"🎯 BBox Koordinaten erhalten: [{bbox_x1}, {bbox_y1}, {bbox_x2}, {bbox_y2}]")
# === WICHTIGE ÄNDERUNG: SAM 2 STATT create_face_mask ===
# 1. MASKE mit SAM 2 erzeugen (transparent für Benutzer)
processed_mask, raw_mask, binary_mask = controlnet_processor.create_sam_mask(
image=image,
bbox_coords=(bbox_x1, bbox_y1, bbox_x2, bbox_y2),
mode=mode,
is_front_face=is_front_face,
is_back_head=is_back_head
)
original_mask = processed_mask
# 2. BILD UND MASKE GEMEINSAM SKALIEREN (mit Padding)
scaled_image, scaled_mask_inpaint, scaled_mask_composite, padding_info = scale_image_and_mask_together(
image.convert("RGB"), # Originalbild
binary_mask, # SAM 2 Binärmaske ohne Blur
original_mask, # SAM 2 Maske geglättet (oder Fallback)
target_size=IMG_SIZE,
bbox_coords=(bbox_x1, bbox_y1, bbox_x2, bbox_y2),
mode=mode
)
print(f"✅ Gemeinsame Skalierung abgeschlossen")
print(f" Original: {image.size} → Skaliert: {scaled_image.size}")
else:
# Keine BBox: Normales Img2Img (ohne Maske)
print(f"ℹ️ Keine BBox angegeben → normales Img2Img (ohne Maske)")
scaled_image = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE), Image.Resampling.LANCZOS)
scaled_mask = Image.new("L", (IMG_SIZE, IMG_SIZE), 255) # Volle Maske
padding_info = None
progress(0.1, desc="ControlNet läuft...")
# ===== CONTROLNET: MAPS ERSTELLEN =====
print(f"📊 ControlNet Input Größe: {scaled_image.size}")
controlnet_maps, debug_maps = controlnet_processor.prepare_controlnet_maps(
image=scaled_image,
keep_environment=keep_environment
)
print(f"✅ ControlNet Maps erstellt: {len(controlnet_maps)} Maps")
progress(0.3, desc="ControlNet abgeschlossen – starte Inpaint...")
# ===== CONTROLNET-INPAINTING PIPELINE ===== Laden der Pipeline!
pipe = load_img2img(keep_environment=keep_environment)
# ===== SEED UND GENERATOR =====
adj_guidance = min(guidance_scale, 12.0)
seed = random.randint(0, 2**32 - 1)
generator = torch.Generator(device=device).manual_seed(seed)
print(f"🌱 Inpaint Seed: {seed}")
# ===== FORTSCHRITTS-CALLBACK =====
callback = ImageToImageProgressCallback(progress, int(steps), adj_strength)
# ===== CONTROLNET-GESTEUERTES INPAINTING DURCHFÜHREN =====
print(f"🔄 Führe ControlNet-gesteuertes Inpainting durch...")
result = pipe(
prompt=enhanced_prompt,
negative_prompt=combined_negative_prompt,
image=scaled_image,
mask_image=scaled_mask_inpaint,
#mask_image=scaled_mask,
control_image=controlnet_maps,
controlnet_conditioning_scale=conditioning_scale, # DYNAMISCHE Liste
strength=adj_strength,
num_inference_steps=int(steps),
guidance_scale=adj_guidance,
generator=generator,
callback_on_step_end=callback,
callback_on_step_end_tensor_inputs=[],
)
print("✅ ControlNet-Inpainting abgeschlossen")
# ===== KORREKTES COMPOSITING =====
generated_image = result.images[0]
if original_mask is not None and padding_info is not None:
# KORREKTER WORKFLOW: Nur bearbeiteten Bereich in Originalbild einfügen
final_image = enhanced_composite_with_sam(
original_image=image.convert("RGB"),
inpaint_result=generated_image,
original_mask=original_mask,
padding_info=padding_info,
bbox_coords=(bbox_x1, bbox_y1, bbox_x2, bbox_y2),
mode=mode
)
print(f"✅ Korrektes Compositing durchgeführt")
else:
# Keine Maske: Einfach das generierte Bild zurückgeben
final_image = generated_image
mask_preview = Image.new("RGB", (512, 512), color="gray")
raw_sam_mask_display = Image.new("RGB", (512, 512), color="gray")
controlnet_map1 = Image.new("RGB", (512, 512), color="gray")
controlnet_map2 = Image.new("RGB", (512, 512), color="gray")
print(f"ℹ️ Keine Maske → Direkte Rückgabe des Bildes")
end_time = time.time()
duration = end_time - start_time
print(f"✅ Transformation abgeschlossen in {duration:.2f} Sekunden")
print(f"🎯 Verwendeter Modus: {mode}")
print(f"⚙️ ControlNet: {'Depth+Canny' if keep_environment else 'OpenPose+Canny'}")
print(f"📊 Finale Bildgröße: {final_image.size}")
# 1. Maske in RGB für die Anzeige konvertieren
mask_preview = original_mask.convert("RGB")
raw_sam_mask_display = raw_mask.convert("RGB")
if "pose" in debug_maps:
controlnet_map1 = debug_maps["pose"]
map1_label = "🎭 Pose Map"
else:
controlnet_map1 = debug_maps["depth"]
map1_label = "🏔️ Depth Map"
controlnet_map2 = debug_maps["canny"]
# Return 5 Werte:
return final_image, raw_sam_mask_display, mask_preview, controlnet_map1, controlnet_map2
except Exception as e:
print(f"❌ Fehler in img_to_image: {e}")
import traceback
traceback.print_exc()
# Fallback: Return das Originalbild oder ein leeres Bild
if image is not None:
fallback_image = image.copy()
else:
fallback_image = Image.new("RGB", (512, 512), color="gray")
return final_image, None, None, None, None
def update_bbox_from_image(image):
"""Aktualisiert die Bounding-Box-Koordinaten wenn ein Bild hochgeladen wird"""
if image is None:
return None, None, None, None
bbox = auto_detect_face_area(image)
return bbox[0], bbox[1], bbox[2], bbox[3]
def update_model_settings(model_id):
"""Aktualisiert die empfohlenen Einstellungen basierend auf Modellauswahl"""
config = MODEL_CONFIGS.get(model_id, MODEL_CONFIGS["runwayml/stable-diffusion-v1-5"])
return (
config["recommended_steps"], # steps
config["recommended_cfg"], # guidance_scale
f"📊 Empfohlene Einstellungen: {config['recommended_steps']} Steps, CFG {config['recommended_cfg']}"
)
def main_ui():
"""
HAUPT-UI (ANGEPASST FÜR 3 MODI)
"""
with gr.Blocks(
title="AI Image Generator",
theme=gr.themes.Base(),
css="""
/* ===== INFO-BOXEN über Textboxen ===== */
.info-box {
background: #f8fafc;
padding: 8px 12px;
border-radius: 6px;
border: 2px solid #e2e8f0;
margin-bottom: 6px;
font-size: 12px;
line-height: 1.3;
min-height: 50px !important;
height: 50px !important;
display: flex !important;
align-items: center;
justify-content: flex-start !important;
text-align: left;
padding-left: 15px;
overflow: hidden !important; /* KEIN Scroll */
border: none !important;
}
/* Linke Box (Prompt) - Blau */
.gr-column:first-child .info-box {
border-left: 4px solid #3b82f6;
background: #eff6ff;
}
/* Rechte Box (Negativ) - Rot */
.gr-column:last-child .info-box {
border-left: 4px solid #ef4444;
background: #fef2f2;
}
/* Code in Info-Boxen */
.info-box code {
background: white;
padding: 3px 3px;
border-radius: 4px;
font-family: monospace;
font-size: 12px;
border: 1px solid #e2e8f0;
display: inline-block;
margin: 3px 0;
}
/* ===== TEXTBOXEN ===== */
.prompt-box textarea {
min-height: 90px !important;
border-radius: 6px !important;
border: 2px solid #e2e8f0 !important;
padding: 10px !important;
font-size: 14px !important;
}
/* Focus-State */
.prompt-box textarea:focus {
border-color: #3b82f6 !important;
outline: none !important;
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1) !important;
}
/* Platzhalter */
.prompt-box textarea::placeholder {
color: #94a3b8 !important;
}
.clickable-file {
color: #1976d2;
cursor: pointer;
text-decoration: none;
font-family: 'Monaco', 'Consolas', monospace;
background: #e3f2fd;
padding: 2px 6px;
border-radius: 4px;
border: 1px solid #bbdefb;
}
.clickable-file:hover {
background: #bbdefb;
text-decoration: underline;
}
.model-info-box {
background: #e8f4fd;
padding: 12px;
border-radius: 6px;
margin: 10px 0;
border-left: 4px solid #2196f3;
font-size: 14px;
}
#generate-button {
background-color: #0080FF !important;
border: none !important;
margin: 20px auto !important;
display: block !important;
font-weight: 600;
width: 280px;
}
#generate-button:hover {
background-color: #0066CC !important;
}
.hint-box {
margin-top: 20px;
}
.custom-text {
font-size: 25px !important;
}
.image-upload .svelte-1p4f8co {
display: block !important;
}
.preview-box {
border: 2px dashed #ccc;
padding: 10px;
border-radius: 8px;
margin: 10px 0;
}
.mode-red {
border: 3px solid #ff4444 !important;
}
.mode-green {
border: 3px solid #44ff44 !important;
}
.coordinate-sliders {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
margin: 10px 0;
}
.gr-checkbox .wrap .text-gray {
font-size: 14px !important;
font-weight: 600 !important;
line-height: 1.4 !important;
}
.status-message {
padding: 10px;
border-radius: 5px;
margin: 10px 0;
text-align: center;
font-weight: 500;
}
.status-success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.radio-group {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
margin: 10px 0;
border: 2px solid #e9ecef;
}
.radio-item {
padding: 8px 12px;
margin: 5px 0;
border-radius: 4px;
transition: background 0.3s;
}
.radio-item:hover {
background: #e9ecef;
}
.radio-label {
font-weight: 600;
font-size: 14px;
}
.radio-description {
font-size: 12px;
color: #6c757d;
margin-left: 24px;
}
"""
) as demo:
with gr.Column(visible=True) as content_area:
with gr.Tab("Text zu Bild"):
gr.Markdown("## 🎨 Text zu Bild Generator")
with gr.Row():
with gr.Column(scale=2):
# Modellauswahl Dropdown (NUR 2 MODELLE)
model_dropdown = gr.Dropdown(
choices=[
(config["name"], model_id)
for model_id, config in MODEL_CONFIGS.items()
],
value="runwayml/stable-diffusion-v1-5",
label="📁 Modellauswahl",
info="🏠 Universal vs 👤 Portraits"
)
# Modellinformationen Box
model_info_box = gr.Markdown(
value="<div class='model-info-box'>"
"**🏠 Stable Diffusion 1.5 (Universal)**<br>"
"Universal model, good all-rounder, reliable results<br>"
"Empfohlene Einstellungen: 35 Steps, CFG 7.5"
"</div>",
label="Modellinformationen"
)
with gr.Column(scale=3):
txt_input = gr.Textbox(
placeholder="z.B. ultra realistic mountain landscape at sunrise, soft mist over the valley, detailed foliage, crisp textures, depth of field, sunlight rays through clouds, shot on medium format camera, 8k, HDR, hyper-detailed, natural lighting, masterpiece",
lines=3,
label="🎯 Prompt (Englisch)",
info="Beschreibe detailliert, was du sehen möchtest. Negative Prompts werden automatisch generiert."
)
with gr.Row():
with gr.Column():
txt_steps = gr.Slider(
minimum=10, maximum=100, value=35, step=1,
label="⚙️ Inferenz-Schritte",
info="Mehr Schritte = bessere Qualität, aber langsamer (20-50 empfohlen)"
)
with gr.Column():
txt_guidance = gr.Slider(
minimum=1.0, maximum=20.0, value=7.5, step=0.5,
label="🎛️ Prompt-Stärke (CFG Scale)",
info="Wie stark der Prompt befolgt wird (7-12 für gute Balance)"
)
# Status-Nachricht
status_output = gr.Markdown(
value="",
elem_classes="status-message"
)
generate_btn = gr.Button("🚀 Bild generieren", variant="primary", elem_id="generate-button")
with gr.Row():
txt_output = gr.Image(
label="🖼️ Generiertes Bild",
show_download_button=True,
type="pil",
height=400
)
# Event-Handler für Modelländerung
def update_model_info(model_id):
config = MODEL_CONFIGS.get(model_id, MODEL_CONFIGS["runwayml/stable-diffusion-v1-5"])
info_html = f"""
<div class='model-info-box'>
<strong>{config['name']}</strong><br>
{config['description']}<br>
<em>Empfohlene Einstellungen: {config['recommended_steps']} Steps, CFG {config['recommended_cfg']}</em>
</div>
"""
return info_html, config["recommended_steps"], config["recommended_cfg"]
model_dropdown.change(
fn=update_model_info,
inputs=[model_dropdown],
outputs=[model_info_box, txt_steps, txt_guidance]
)
generate_btn.click(
fn=text_to_image,
inputs=[txt_input, model_dropdown, txt_steps, txt_guidance],
outputs=[txt_output, status_output],
concurrency_limit=1
)
with gr.Tab("Bild zu Bild"):
gr.Markdown("## 🖼️ Bild zu Bild Transformation (3 MODI)")
with gr.Row():
with gr.Column():
img_input = gr.Image(
type="pil",
label="📤 Eingabebild",
height=300,
sources=["upload"],
elem_id="image-upload"
)
with gr.Column():
preview_output = gr.Image(
label="🎯 Live-Vorschau mit Maske",
height=300,
interactive=False,
show_download_button=False
)
# ===== NEUE RADIO-BUTTONS STATT CHECKBOX =====
with gr.Row():
with gr.Column():
gr.Markdown("### 🎛️ Transformations-Modus")
# NEU: 3 Radio-Buttons statt 1 Checkbox
mode_radio = gr.Radio(
choices=[
("🌳 Umgebung ändern", "environment_change"),
("🎯 Focus verändern", "focus_change"),
("👤 Ausschließlich Gesicht", "face_only_change")
],
value="environment_change", # Standardmodus
label="Wähle den Transformationsmodus:",
info="Steuert, welcher Teil des Bildes verändert wird",
elem_classes="radio-group"
)
# Detailierte Erklärungen
gr.Markdown("""
<div style="font-size: 12px; color: #666; margin-top: 10px;">
<strong>Modus-Erklärungen:</strong><br>
• <strong>🌳 Umgebung ändern:</strong> Ändert alles AUSSER dem markierten Bereich (Depth+Canny)<br>
• <strong>🎯 Focus verändern:</strong> Ändert markierten Bereich+Körper (OpenPose+Canny)<br>
• <strong>👤 Ausschließlich Gesicht:</strong> Ändert NUR den markierten Bereich (Depth+Canny)
</div>
""")
with gr.Row():
gr.Markdown("### 📐 Bildelementbereich anpassen")
# SLIDER MIT DYNAMISCHEM MAXIMUM (4096 für große Bilder)
with gr.Row():
with gr.Column():
bbox_x1 = gr.Slider(
label="← Links (x1)",
minimum=0, maximum=MAX_IMAGE_SIZE, value=100, step=1,
info="Linke Kante des Bildelementbereichs"
)
with gr.Column():
bbox_y1 = gr.Slider(
label="↑ Oben (y1)",
minimum=0, maximum=MAX_IMAGE_SIZE, value=100, step=1,
info="Obere Kante des Bildelementbereichs"
)
with gr.Row():
with gr.Column():
bbox_x2 = gr.Slider(
label="→ Rechts (x2)",
minimum=0, maximum=MAX_IMAGE_SIZE, value=300, step=1,
info="Rechte Kante des Bildelementbereichs"
)
with gr.Column():
bbox_y2 = gr.Slider(
label="↓ Unten (y2)",
minimum=0, maximum=MAX_IMAGE_SIZE, value=300, step=1,
info="Untere Kante des Bildelementbereichs"
)
with gr.Row():
with gr.Column():
# Info-Block über Positiv-Prompt
pos_info = gr.Markdown(
value="`[STIL-MOTIV], [UMGEBUNG], [VOR/HINTERGRUND], [DETAILS], [QUALITÄT], [BELEUCHTUNG]`",
elem_classes=["info-box"]
)
img_prompt = gr.Textbox(
placeholder="photorealistic coastal beach, keep person unchanged, high detailed, diffused light",
lines=2,
label="🎯 Transformations-Prompt (Englisch)",
#info="Was soll verändert werden? Sei spezifisch."
elem_classes=["prompt-box"]
)
with gr.Column():
# Info_Block über Negativ-Prompt
neg_info = gr.Markdown(
value="`[GESICHTER/ANATOMIE], [FEHLER], [QUALITÄT], [UNERWÜNSCHTES]`",
elem_classes=["info-box"]
)
img_neg_prompt = gr.Textbox(
placeholder="blurry face, deformed anatomy, ugly, extra limbs, poorly drawn hands",
lines=2,
label="🚫 Negativ-Prompt (Englisch)",
#info="Was soll vermieden werden? Unerwünschte Elemente auflisten."
elem_classes=["prompt-box"]
)
with gr.Row():
with gr.Column():
strength_slider = gr.Slider(
minimum=0.1, maximum=0.9, value=0.4, step=0.05,
label="💪 Veränderungs-Stärke (strength)",
info="0.1-0.3: Leichte Anpassungen, 0.4-0.6: Mittlere Veränderungen, 0.7-0.9: Starke Umgestaltung"
)
with gr.Column():
img_steps = gr.Slider(
minimum=10, maximum=45, value=35, step=1,
label="⚙️ Inferenz-Schritte",
info="Anzahl der Verarbeitungsschritte (25-45 für gute Ergebnisse)"
)
with gr.Column():
img_guidance = gr.Slider(
minimum=1.0, maximum=15.0, value=7.5, step=0.5,
label="🎛️ Prompt-Stärke (guidance)",
info="Einfluss des Prompts auf das Ergebnis (6-10 für natürliche Ergebnisse)"
)
with gr.Row():
gr.Markdown(
"### 📋 Hinweise:\n"
"• **🆕 3 Transformations-Modi** für präzise Kontrolle\n"
"• **🆕 Unterstützt Bilder bis 4096×4096 Pixel**\n"
"• **🆕 Automatische Bildelementerkennung** setzt Koordinaten beim Upload\n"
"• **🆕 Live-Vorschau** zeigt farbige Rahmen je nach Modus\n"
"• **🆕 Dynamische Koordinaten-Schieberegler** passen sich an Bildgröße an\n"
"• **ControlNet-Technologie** für konsistente Ergebnisse\n"
"• **Automatische Negative Prompts** für bessere Qualität\n"
"• **KORREKTER COMPOSITING-WORKFLOW** – nur bearbeiteter Bereich wird eingefügt\n"
"• **Ausgabe in Eingabebildgröße"
)
transform_btn = gr.Button("🔄 Bild transformieren", variant="primary")
with gr.Row():
img_output = gr.Image(
label="✨ Transformiertes Bild",
show_download_button=True,
type="pil",
height=400
)
with gr.Row():
sam_raw_mask_output = gr.Image(
label="🔍 SAM-Rohmaske (Vor Nachbearbeitung)",
type="pil",
height=300,
show_download_button=False
)
processed_mask_output = gr.Image(
label="🛠️ Nachbearbeitete Maske (Für Inpainting)",
type="pil",
height=300,
show_download_button=False
)
with gr.Row():
pose_map_output = gr.Image(
label="🎭 Pose/Depth Map",
type="pil",
height=300,
show_download_button=False
)
canny_map_output = gr.Image(
label="📐 Canny Edge Map",
type="pil",
height=300,
show_download_button=False
)
# EVENT-HANDLER FÜR DYNAMISCHE BILDGRÖßEN
img_input.upload(
fn=process_image_upload,
inputs=[img_input],
outputs=[preview_output, bbox_x1, bbox_y1, bbox_x2, bbox_y2]
).then(
fn=update_slider_for_image,
inputs=[img_input],
outputs=[bbox_x1, bbox_y1, bbox_x2, bbox_y2]
)
# NEUE Input-Liste mit mode_radio statt face_preserve
coordinate_inputs = [img_input, bbox_x1, bbox_y1, bbox_x2, bbox_y2, mode_radio]
# Live-Vorschau Updates für alle Steuerelemente
for slider in [bbox_x1, bbox_y1, bbox_x2, bbox_y2]:
slider.release(
fn=update_live_preview,
inputs=coordinate_inputs,
outputs=preview_output
)
# 2. PLATZHALTER-FUNKTION
def update_info(mode):
if mode == "environment_change":
return (
"`[STIL-MOTIV], [UMGEBUNG], [VOR/HINTERGRUND], [DETAILS], [QUALITÄT], [BELEUCHTUNG]`",
"`[GESICHTER/ANATOMIE], [FEHLER], [QUALITÄT], [UNERWÜNSCHTES]`"
)
elif mode == "focus_change":
return (
"`[GESICHTSBESCHREIBUNG], [KLEIDUNG], [POSITION], [DETAILS], [STIL]`",
"`[DEFORMIERT], [UNSCHÄRFE], [ANATOMIEFEHLER], [UNERWÜNSCHTES]`"
)
else: # face_only_change
return (
"`[HAARFARBE], [AUGEN], [GESICHTSAUSDRUCK], [DETAILS], [BELEUCHTUNG]`",
"`[UNREALISTISCH], [ASYMETRISCH], [FEHLER], [UNERWÜNSCHTES]`"
)
# 3. EVENT-HANDLER zur Änderung Textbox-Info- Verbindung Text-Box und Funktion
mode_radio.change(
fn=update_info,
inputs=[mode_radio],
outputs=[pos_info, neg_info]
)
# Mode-Radio-Button ändert auch Live-Vorschau
mode_radio.change(
fn=update_live_preview,
inputs=coordinate_inputs,
outputs=preview_output
)
# NEU: Transform-Button mit mode_radio statt face_preserve
transform_btn.click(
fn=img_to_image,
inputs=[
img_input, img_prompt, img_neg_prompt,
strength_slider, img_steps, img_guidance,
mode_radio, bbox_x1, bbox_y1, bbox_x2, bbox_y2
],
outputs=[img_output, sam_raw_mask_output, processed_mask_output, pose_map_output, canny_map_output],
concurrency_limit=1
)
return demo |