My other answer is for orthographic projection of the cube. This one is for perspective projection.
It is assumed that the image processing algorithm knows the coordinates of the four corners of the face $P_1, P_2, P_3, P_4$ ordered in clockwise or counter clockwise direction. In addition it is assumed that the two vertices $P_5$ connected to $P_1$ and $P_6$ connected to $P_2$ also have known coordinates.
Each point in the picture corresponds to a line in space. Each of these lines passes through the point $E = (E_x, E_y, 0) $ (unknown) and the point
$Q_i = \begin{bmatrix} P_i \\ z_0 \end{bmatrix} , \ i = 1 , 2, 3, 4$
The focal distance $z_0$ is unknown, and has to be determined.
To determine $E$, we will use the fact that the projection center $E$ is the orthocenter of the three vanishing points in the three directions (left-right, forward-backward, and up-down).
So from the known $6$ vertices we'll be able to determine the projection center $E$.
Having determined $E$, we now have the equation of the lines describing the location of the vertices of the cube face under consideration.
$ L_i(t_i) = E + t_i (Q_i - E) , \ i = 1,2,3,4$
Since we know that the face is a square, then it is a rectangle. A rectangle is a special parallelogram with its corner angles equal to $90^\circ$.
To ensure that the actual points in $3D$ space form a parallelogram, we must have
$ L_2 - L_1 = L_3 - L_4 $
So that,
$ t_2 (Q_2 - E) - t_1 (Q_1 - E) = t_3 (Q_3 - E) - t_4 (Q_4 - E) $
Remember the third coordinate of all the $Q_i$'s is the focal distance $z_0$ and the third coordinate of $E$ is zero. So, from the third component of the above vector equation, we get
$ t_2 - t_1 = t_3 - t_4 $
And using this, $E$ in the first two rows gets eliminated, and the equation for the parallelogram condition becomes
$ t_2 Q_2 - t_1 Q_1 = t_3 Q_3 - t_4 Q_4 $
This is a linear system of $3$ equations in $4$ unknowns. So it can be solved using Gaussian elimination, and it will yields
$ (t_1, t_2, t_3, t_4) = \lambda \mathbf{v} $
Next, we determine $z_0$.
For that, we impose the orthogonality condition on the line segments $L_1 L_2$ and $L_2 L_3$. This means
$ ( t_2 (Q_2 - E) - t_1 (Q_1 - E) ) \cdot ( t_3 (Q_3 - E) - t_2 (Q_2 - E) ) = 0 \hspace{13pt}(1*) $
Using $Q_i = \begin{bmatrix} P_i \\ z_0 \end{bmatrix} $ and $t_i = \lambda v_i $
This becomes
$ ( v_2 (P_2 - E) - v_1 (P_1 - E) ) \cdot ( v_3 (P_3 - E) - v_2 (P2 - E) ) + (t_1 - t_2)(t_2 - t_3) z_0^2 = 0 $
Everything in this equation is known except $z_0$. Solving for $z_0$ , we get
$ z_0 = - \sqrt{ \dfrac{( v_2 (P_2 - E) - v_1 (P_1 - E) ) \cdot ( v_3 (P_3 - E) - v_2 (P2 - E) ) }{ (t_1 - t_2)(t_3 - t_2) }} $
Having determine $E$ and $z_0$, we can now find the normal vector to the face as follows
$N = ( L_3 - L_2 ) \times (L_2 - L_1 ) $
where $\times$ denotes cross multiplication.
