Sephfox commited on
Commit
b1ed649
1 Parent(s): a69e1fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -214
app.py CHANGED
@@ -414,7 +414,7 @@ mp_pose = mp.solutions.pose
414
  pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
415
 
416
  def detect_humanoid(image_path):
417
- image = imread(image_path)
418
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
419
  results = pose.process(image_rgb)
420
  if results.pose_landmarks:
@@ -429,7 +429,9 @@ def detect_humanoid(image_path):
429
  return []
430
 
431
  def apply_touch_points(image_path, keypoints):
432
- image = imread(image_path)
 
 
433
  draw = ImageDraw.Draw(image)
434
  for point in keypoints:
435
  draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill='red')
@@ -462,10 +464,6 @@ def create_sensation_map(width, height, keypoints):
462
 
463
  return sensation_map
464
 
465
- def create_heatmap(sensation_map, sensation_type):
466
- plt.figure(figsize=(10, 15))
467
- sns.heatmap(sensation_map[:, :, sensation_type], cmap='viridis')
468
-
469
  def create_heatmap(sensation_map, sensation_type):
470
  plt.figure(figsize=(10, 15))
471
  sns.heatmap(sensation_map[:, :, sensation_type], cmap='viridis')
@@ -494,34 +492,27 @@ def generate_ai_response(keypoints, sensation_map):
494
 
495
  return response
496
 
497
- # Create and display avatar with heatmap
498
- st.subheader("Avatar with Sensation Heatmap")
499
-
500
- # You need to define sensation_map and sensation_type before this
501
- sensation_map = np.random.rand(AVATAR_HEIGHT, 600, AVATAR_WIDTH, 300) # Example random sensation map
502
- sensation_type = 0 # Example sensation type (0 for Pain)
503
-
504
- avatar_with_heatmap = create_avatar_with_heatmap(sensation_map, sensation_type)
505
- st.image(avatar_with_heatmap, use_column_width=True)
506
-
507
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
508
 
509
  if uploaded_file is not None:
510
  # Read the image
511
- image = Image.open(uploaded_file)
512
- image_np = np.array(image)
 
513
 
514
  # Detect humanoid keypoints
515
- keypoints = detect_humanoid(image_np)
516
 
517
  # Apply touch points to the image
518
- processed_image = apply_touch_points(image.copy(), keypoints)
519
 
520
  # Display the processed image
521
  st.image(processed_image, caption='Processed Image with Touch Points', use_column_width=True)
522
 
523
  # Create sensation map
524
- sensation_map = create_sensation_map(image.width, image.height, keypoints)
 
 
525
 
526
  # Display heatmaps for different sensations
527
  sensation_types = ["Pain", "Pleasure", "Pressure", "Temperature", "Texture", "EM Field",
@@ -535,199 +526,8 @@ if uploaded_file is not None:
535
  if st.button("Generate AI Response"):
536
  response = generate_ai_response(keypoints, sensation_map)
537
  st.write("AI Response:", response)
538
-
539
-
540
-
541
-
542
-
543
- # Create futuristic human-like avatar
544
- def create_avatar():
545
- img = Image.new('RGBA', (AVATAR_WIDTH, AVATAR_HEIGHT), color=(0, 0, 0, 0))
546
- draw = ImageDraw.Draw(img)
547
-
548
- # Body
549
- draw.polygon([(300, 100), (200, 250), (250, 600), (300, 750), (350, 600), (400, 250)], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
550
-
551
- # Head
552
- draw.ellipse([250, 50, 350, 150], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
553
-
554
- # Eyes
555
- draw.ellipse([275, 80, 295, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
556
- draw.ellipse([305, 80, 325, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
557
-
558
- # Nose
559
- draw.polygon([(300, 90), (290, 110), (310, 110)], fill=(0, 255, 255, 150))
560
-
561
- # Mouth
562
- draw.arc([280, 110, 320, 130], 0, 180, fill=(0, 255, 255, 200), width=2)
563
-
564
- # Arms
565
- draw.line([(200, 250), (150, 400)], fill=(0, 255, 255, 200), width=5)
566
- draw.line([(400, 250), (450, 400)], fill=(0, 255, 255, 200), width=5)
567
-
568
- # Hands
569
- draw.ellipse([140, 390, 160, 410], fill=(0, 255, 255, 150))
570
- draw.ellipse([440, 390, 460, 410], fill=(0, 255, 255, 150))
571
-
572
- # Fingers
573
- for i in range(5):
574
- draw.line([(150 + i*5, 400), (145 + i*5, 420)], fill=(0, 255, 255, 200), width=2)
575
- draw.line([(450 - i*5, 400), (455 - i*5, 420)], fill=(0, 255, 255, 200), width=2)
576
-
577
- # Legs
578
- draw.line([(250, 600), (230, 780)], fill=(0, 255, 255, 200), width=5)
579
- draw.line([(350, 600), (370, 780)], fill=(0, 255, 255, 200), width=5)
580
-
581
- # Feet
582
- draw.ellipse([220, 770, 240, 790], fill=(0, 255, 255, 150))
583
- draw.ellipse([360, 770, 380, 790], fill=(0, 255, 255, 150))
584
-
585
- # Toes
586
- for i in range(5):
587
- draw.line([(225 + i*3, 790), (223 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
588
- draw.line([(365 + i*3, 790), (363 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
589
-
590
- def generate_neural_network_lines(img, draw):
591
- # Neural network lines
592
- for _ in range(100):
593
- start = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
594
- end = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
595
- draw.line([start, end], fill=(0, 255, 255, 50), width=1)
596
- return img
597
-
598
-
599
- # Create and display avatar with heatmap
600
- st.subheader("Avatar with Sensation Heatmap")
601
- avatar_with_heatmap = create_avatar_with_heatmap()
602
- st.image(avatar_with_heatmap, use_column_width=True)
603
- # Create avatar function
604
- def create_avatar():
605
- img = Image.new('RGBA', (AVATAR_WIDTH, AVATAR_HEIGHT), color=(0, 0, 0, 0))
606
- draw = ImageDraw.Draw(img)
607
-
608
- # Body
609
- draw.polygon([(300, 100), (200, 250), (250, 600), (300, 750), (350, 600), (400, 250)], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
610
-
611
- # Head
612
- draw.ellipse([250, 50, 350, 150], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
613
-
614
- # Eyes
615
- draw.ellipse([275, 80, 295, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
616
- draw.ellipse([305, 80, 325, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
617
-
618
- # Nose
619
- draw.polygon([(300, 90), (290, 110), (310, 110)], fill=(0, 255, 255, 150))
620
-
621
- # Mouth
622
- draw.arc([280, 110, 320, 130], 0, 180, fill=(0, 255, 255, 200), width=2)
623
-
624
- # Arms
625
- draw.line([(200, 250), (150, 400)], fill=(0, 255, 255, 200), width=5)
626
- draw.line([(400, 250), (450, 400)], fill=(0, 255, 255, 200), width=5)
627
-
628
- # Hands
629
- draw.ellipse([140, 390, 160, 410], fill=(0, 255, 255, 150))
630
- draw.ellipse([440, 390, 460, 410], fill=(0, 255, 255, 150))
631
-
632
- # Fingers
633
- for i in range(5):
634
- draw.line([(150 + i*5, 400), (145 + i*5, 420)], fill=(0, 255, 255, 200), width=2)
635
- draw.line([(450 - i*5, 400), (455 - i*5, 420)], fill=(0, 255, 255, 200), width=2)
636
-
637
- # Legs
638
- draw.line([(250, 600), (230, 780)], fill=(0, 255, 255, 200), width=5)
639
- draw.line([(350, 600), (370, 780)], fill=(0, 255, 255, 200), width=5)
640
-
641
- # Feet
642
- draw.ellipse([220, 770, 240, 790], fill=(0, 255, 255, 150))
643
- draw.ellipse([360, 770, 380, 790], fill=(0, 255, 255, 150))
644
-
645
- # Toes
646
- for i in range(5):
647
- draw.line([(225 + i*3, 790), (223 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
648
- draw.line([(365 + i*3, 790), (363 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
649
-
650
- # Neural network lines
651
- for _ in range(100):
652
- start = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
653
- end = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
654
- draw.line([start, end], fill=(0, 255, 255, 50), width=1)
655
-
656
- return img
657
- def create_avatar_with_heatmap(image_path, show_heatmap=True):
658
- # Load the image
659
- avatar_img = Image.open(image_path)
660
-
661
- if not show_heatmap:
662
- return avatar_img
663
-
664
- # Create a heatmap
665
- heatmap_img = create_heatmap(sensation_map)
666
-
667
- # Resize heatmap to match avatar size
668
- heatmap_img = heatmap_img.resize((image.width, image.height))
669
-
670
- # Adjust alpha channel of heatmap
671
- data = np.array(heatmap_img)
672
- if data.shape[2] == 3: # If RGB, add an alpha channel
673
- data = np.concatenate([data, np.full((data.shape[0], data.shape[1], 1), 255, dtype=np.uint8)], axis=2)
674
- data[:, :, 3] = data[:, :, 3] * 0.5 # Reduce opacity to 50%
675
- heatmap_img = Image.fromarray(data)
676
-
677
- # Combine avatar and heatmap
678
- combined_img = Image.alpha_composite(avatar_img.convert('RGBA'), heatmap_img.convert('RGBA'))
679
- return combined_img
680
-
681
- # Create and display avatar with optional heatmap
682
- st.subheader("Avatar with Optional Sensation Heatmap")
683
- avatar_with_heatmap = create_avatar_with_heatmap(show_heatmap)
684
- st.image(avatar_with_heatmap, use_column_width=True)
685
-
686
- # Load the chosen humanoid image
687
- image_path = 'chosen_avatar.jpg'
688
- keypoints = detect_humanoid(image_path)
689
- image_with_touch_points = apply_touch_points(image_path, keypoints)
690
- heatmap_avatar = create_avatar_with_heatmap(image_path)
691
-
692
- # Display the images
693
- plt.figure(figsize=(15, 5))
694
- plt.subplot(1, 3, 1)
695
- plt.imshow(image_with_touch_points)
696
- plt.title('Image with Touch Points')
697
-
698
- plt.subplot(1, 3, 2)
699
- plt.imshow(heatmap_avatar)
700
- plt.title('Avatar with Heatmap')
701
-
702
- plt.show()
703
-
704
- # Create three columns
705
- col1, col2, col3 = st.columns(3)
706
-
707
-
708
-
709
- # Avatar display with touch interface
710
- with col1:
711
- st.subheader("Humanoid Avatar Interface")
712
-
713
- # Use st_canvas for touch input
714
- canvas_result = st_canvas(
715
- fill_color="rgba(0, 255, 255, 0.3)",
716
- stroke_width=2,
717
- stroke_color="#00FFFF",
718
- background_image=avatar_with_heatmap,
719
- height=AVATAR_HEIGHT,
720
- width=AVATAR_WIDTH,
721
- drawing_mode="point",
722
- key="canvas",
723
- )
724
-
725
-
726
- with col3:
727
- st.subheader("Sensation Heatmap")
728
- heatmap = create_heatmap(avatar_sensation_map)
729
- st.image(heatmap, use_column_width=True)
730
- # Touch controls and output
731
  with col2:
732
  st.subheader("Neural Interface Controls")
733
 
 
414
  pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
415
 
416
  def detect_humanoid(image_path):
417
+ image = cv2.imread(image_path)
418
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
419
  results = pose.process(image_rgb)
420
  if results.pose_landmarks:
 
429
  return []
430
 
431
  def apply_touch_points(image_path, keypoints):
432
+ image = cv2.imread(image_path)
433
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
434
+ image = Image.fromarray(image)
435
  draw = ImageDraw.Draw(image)
436
  for point in keypoints:
437
  draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill='red')
 
464
 
465
  return sensation_map
466
 
 
 
 
 
467
  def create_heatmap(sensation_map, sensation_type):
468
  plt.figure(figsize=(10, 15))
469
  sns.heatmap(sensation_map[:, :, sensation_type], cmap='viridis')
 
492
 
493
  return response
494
 
 
 
 
 
 
 
 
 
 
 
495
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
496
 
497
  if uploaded_file is not None:
498
  # Read the image
499
+ image_path = 'temp.jpg'
500
+ with open(image_path, 'wb') as f:
501
+ f.write(uploaded_file.getvalue())
502
 
503
  # Detect humanoid keypoints
504
+ keypoints = detect_humanoid(image_path)
505
 
506
  # Apply touch points to the image
507
+ processed_image = apply_touch_points(image_path, keypoints)
508
 
509
  # Display the processed image
510
  st.image(processed_image, caption='Processed Image with Touch Points', use_column_width=True)
511
 
512
  # Create sensation map
513
+ image = cv2.imread(image_path)
514
+ image_height, image_width, _ = image.shape
515
+ sensation_map = create_sensation_map(image_width, image_height, keypoints)
516
 
517
  # Display heatmaps for different sensations
518
  sensation_types = ["Pain", "Pleasure", "Pressure", "Temperature", "Texture", "EM Field",
 
526
  if st.button("Generate AI Response"):
527
  response = generate_ai_response(keypoints, sensation_map)
528
  st.write("AI Response:", response)
529
+
530
+ # Touch controls and output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
  with col2:
532
  st.subheader("Neural Interface Controls")
533