Python copy animation from armature to another armature different rest pose
Make constraints to visualize, then convert.
Not at all sure how the code in question is going to even be close to what you wish to achieve.
Would look at doing something akin to answer here.
PoseBone local rotation values to global with axis changed
The last example of which shows how to run thru pose bones, convert space, change the alignment and convert back.
Create pose from existing armature
This one has two armatures with two distinct rest poses, (the one with animation is an abomination)  To create the animation on t pose rig from other have added constraints to copy the transforms from source to target, then loop thru and create the animation.
For your example since the bones are not using same axes, could instead copy location to but &quot;Incorrect&quot; bones heads at &quot;Correct&quot; bones heads, and use a track to constraint.
Script to constraint target pose bones to source.
import bpy
from math import pi
context = bpy.context
scene = context.scene

source = scene.objects.get(&quot;Correct&quot;)
# Incorrect (sp)
target = scene.objects.get(&quot;Uncorrect&quot;)
target.animation_data_clear() # remove any ad

for pbc, pbi in zip(source.pose.bones, target.pose.bones):
    # clear  for testing
    while pbi.constraints:
        pbi.constraints.remove(pbi.constraints[0])

    
    # locations
    
    cl = pbi.constraints.new('COPY_LOCATION')
    cl.target = pbc.id_data
    cl.subtarget = pbc.name
    cl.owner_space = 'POSE'
    cl.target_space = 'POSE'
    
    # rotations
    
    tt = pbi.constraints.new('TRACK_TO')
    tt.target = pbc.id_data
    tt.subtarget = pbc.name
    tt.owner_space = 'POSE'
    tt.target_space = 'POSE' 
    tt.head_tail = 1
    tt.track_axis = 'TRACK_X'
    tt.up_axis = 'UP_Z'
    tt.use_target_z = True  

Script to convert constraints to keyframe animation
import bpy
from math import pi
context = bpy.context
scene = context.scene

remove_constraints = True

source = scene.objects.get(&quot;Correct&quot;)
# Incorrect (sp)
target = scene.objects.get(&quot;Uncorrect&quot;)

action = source.animation_data.action

f = action.frame_range.x
# add a keyframe to each frame of new rig
while f &lt;= action.frame_range.y:
    scene.frame_set(f)
    context.view_layer.update()
    r2 = target.evaluated_get(context.evaluated_depsgraph_get())
    for pb in target.pose.bones:
        #pb2 = rig1.pose.bones.get(pb.name)
        m = r2.convert_space(
            pose_bone=pb, 
            matrix=pb.matrix, 
            from_space='POSE',
            to_space='LOCAL',
            )
        if pb.rotation_mode == 'QUATERNION':
            pb.rotation_quaternion = m.to_quaternion()
            pb.keyframe_insert(&quot;rotation_quaternion&quot;, frame=f)
        else:

        # add rot mode checking 
            pb.rotation_euler = m.to_euler(pb.rotation_mode)
            pb.keyframe_insert(&quot;rotation_euler&quot;, frame=f)
        pb.location = m.to_translation()

        pb.keyframe_insert(&quot;location&quot;, frame=f)
    f += 1

for pb in target.pose.bones:
    # clear  for testing
    while pb.constraints:
        pb.constraints.remove(pb.constraints[0])