How to get angle of rotation of object

The entire second code snippet is wrong then, I tried to adapt some code I found online the way I thought made sense. I’m quite new to CV and trying to find the most versatile solution to this problem.

On a side note, I read up on Image Moments and Hu moments and how you can get the orientation using second order moments. I also found a practical application on a paper, specifically


   % Central moments (intermediary step) 
    a = E.m20/E.m00 - E.x^2; 
    b = 2*(E.m11/E.m00 - E.x*E.y); 
    c = E.m02/E.m00 - E.y^2;   
    % Orientation (radians)  
    E.theta = 1/2*atan(b/(a-c)) + (a<c)*pi/2;

I adapted it to Python, but the reported angle is still erratic and imprecise. Is this a viable path to get the angle of rotation or is this used for something else completely?